import java.util.*;

public class Bombe {

    public static int maximumDetonatedBombs(int[][] bombs) {
        int total = bombs.length;

        // Napravi graf
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < total; i++) {
            graph.add(new ArrayList<>());
        }

        for (int i = 0; i < total; i++) {
            for (int j = 0; j < total; j++) {
                if (i != j && isInRange(bombs[i], bombs[j])) {
                    graph.get(i).add(j);
                }
            }
        }

        // Nađi maksimalan broj detoniranih bombi
        int maxDetonated = 0;
        for (int i = 0; i < total; i++) {
            maxDetonated = Math.max(maxDetonated, bfs(graph, i, total));
        }

        return maxDetonated;
    }

    // Helper metod koji proverava da li je bomb[j] u dometu bomb[i]
    private static boolean isInRange(int[] bomb1, int[] bomb2) {
        long x1 = bomb1[0], y1 = bomb1[1], r1 = bomb1[2];
        long x2 = bomb2[0], y2 = bomb2[1];
        long distanceSquared = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        return distanceSquared <= r1 * r1;
    }

    // BFS za izračunavanje broja detoniranih bombi počevši od startne bombe
    private static int bfs(List<List<Integer>> graph, int start, int total) {
        boolean[] visited = new boolean[total];
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);
        visited[start] = true;

        int count = 0;
        while (!queue.isEmpty()) {
            int bomb = queue.poll();
            count++;
            for (int neighbor : graph.get(bomb)) {
                if (!visited[neighbor]) {
                    visited[neighbor] = true;
                    queue.add(neighbor);
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {

        // Primer 1
        int[][] bombe1 = {
                {2, 1, 3},
                {6, 1, 4}
        };

        System.out.println("Maksimalan broj detoniranih bombi (1): "
                + maximumDetonatedBombs(bombe1)); // Output: 5


        // Primer 2
        int[][] bombe2 = {
                {1, 1, 5},
                {10, 10, 5}
        };

        System.out.println("Maksimalan broj detoniranih bombi (2): "
                + maximumDetonatedBombs(bombe2)); // Output: 5

        // Primer 3
        int[][] bombe3 = {
                {1, 2, 3},
                {2, 3, 1},
                {3, 4, 2},
                {4, 5, 3},
                {5, 6, 4}
        };

        System.out.println("Maksimalan broj detoniranih bombi (3): "
                + maximumDetonatedBombs(bombe3)); // Output: 5
    }



}
