-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon11403.java
57 lines (48 loc) · 1.52 KB
/
Baekjoon11403.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/11403
* 백준 11403번 경로찾기
*/
public class Baekjoon11403 {
private static int[][] map;
private static int[] visited;
private static int[][] answer;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
map = new int[num][num];
answer = new int[num][num];
for (int i = 0; i < num; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < num; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < num; i++) {
visited = new int[num];
dfs(i);
for (int j = 0; j < num; j++) {
answer[i][j] = visited[j];
}
}
for (int[] arr : answer) {
StringBuilder sb = new StringBuilder();
for (int n : arr) {
sb.append(n).append(" ");
}
System.out.println(sb.toString().trim());
}
}
private static void dfs(int now) {
for (int i = 0; i < map.length; i++) {
if (map[now][i] == 1 && visited[i] != 1) {
visited[i] = 1;
dfs(i);
}
}
}
}