[백준] P11403

동민·2021년 3월 11일
import java.util.Scanner;
 
public class P11403 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), matrix[][] = new int[n][n];
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				System.out.print(dfs(matrix, i, j, new boolean[n]) + " ");
			}
			System.out.println();
		}
		sc.close();
	}
	private static int dfs(int[][] matrix, int start, int target, boolean[] visit) {
		int result = 0;
		if(visit[start] && start == target) return 1;
		
		for(int i = 0; i < matrix.length; i++) {
			if(matrix[start][i] == 1 && !visit[i]) {
				visit[i] = true;
				result = Math.max(result, dfs(matrix, i, target, visit));
			}
		}
		return result;
	}

}
profile
BE Developer

0개의 댓글