BOJ 11403번 경로찾기

|


import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;

public class BOJ_11403 {


    static int[][] adj;
    static int n;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = Integer.parseInt(sc.nextLine());
        adj = new int[n][n];

        for (int i = 0; i < n; i++) {
            adj[i] = Arrays.stream(sc.nextLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
        }

        solve();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(adj[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static void solve() {
        Queue<Integer> queue = new ArrayDeque<>();
        int tmp;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (adj[i][j] == 1) {
                    queue.add(j);
                }
            }

            while (!queue.isEmpty()) {
                int next = queue.poll();
                bfs(i, next, queue);
            }
        }
    }

    private static void bfs(int root, int next, Queue<Integer> queue) {
        for (int i = 0; i < n; i++) {
            if (adj[next][i] == 1 && adj[root][i] != 1) {
                queue.add(i);
                adj[root][i] = 1;
            }
        }

    }


}