[PS] 99ํด๋ฝ ์ฝํ ์คํฐ๋ 17์ผ์ฐจ TIL (์ด์๊ณ์ฐ)
ํ๊ทธ: 99ํด๋ฝ, BFS/DFS, PS, TIL, ์ฝ๋ฉํ ์คํธ์ค๋น, ํญํด99
์นดํ ๊ณ ๋ฆฌ: PS
๋ฌธ์
์ค๋ช
๊ฐ๊ณ๋์์ ๋ ์ฌ๋๊ฐ์ ์ด์๋ฅผ ๊ณ์ฐํ๋ ๋ฌธ์ . ์ฒซ๋ฒ์งธ ์์ ๋ก ์ฃผ์ด์ง ์ด์์ ๋ํด 2์ฐจ์ ๋ฐฐ์ด๋ก ํ์ํ๋ฉด ์๋์ ๊ฐ์ ๊ทธ๋ฆผ์ด ๋๋ค.
ํ์ด (DFS)
๊ด๊ณ๋ฅผ ์๋ฐฉํฅ์ผ๋ก ํ์ํ๊ณ ๋ฐฉ๋ฌธํ ๋
ธ๋์ ๋ํด์๋ visited ๋ฐฐ์ด์ ๊ฐ์ true ๋ก ๋ฐ๊ฟ์ค๋ค.
ํ๋ฒ ํธ์ถ๋ ๋ ๋ง๋ค depth ๋ฅผ ์ฆ๊ฐ์ํจ๋ค. ์ด ๊ฐ์ด ์ฌ๊ธฐ์ ์ด์๊ฐ ๋๋ค.
7๋ฒ ์ฌ๋๊ณผ 3๋ฒ ์ฌ๋์ ์ด์๋ฅผ ๊ตฌํ๋ ค๋ฉด 7 -> 2 -> 1 -> 3
์์ผ๋ก ํ์ํ๋ฉด ๋๋ค. 3๋ฒ ์ฌ๋๊น์ง ํ์์ด ์๋ฃ๋๋ฉด depth ๋ฅผ ๋ฆฌํดํ๋ค.
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
public class Main {
static boolean[][] graph;
static boolean[] visited;
static int answer = -1;
static int n, m;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
graph = new boolean[n + 1][n + 1];
visited = new boolean[n + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
int p1 = Integer.parseInt(st.nextToken());
int p2 = Integer.parseInt(st.nextToken());
m = Integer.parseInt(br.readLine());
int x, y;
for (int i = 0; i < m; i++) {
StringTokenizer kinship = new StringTokenizer(br.readLine());
x = Integer.parseInt(kinship.nextToken());
y = Integer.parseInt(kinship.nextToken());
// ์๋ก ๊ด๊ณ๋ฅผ ๋งบ์ด์ค๋ค
graph[x][y] = true;
graph[y][x] = true;
}
dfs(p1, p2, 0);
System.out.println(answer);
}
private static void dfs(int p1, int p2, int depth) {
if (p1 == p2) {
answer = depth;
return;
}
visited[p1] = true;
for (int j = 1; j <= n; j++) {
if (!visited[j] && graph[p1][j]) { // ๋ฐฉ๋ฌธํ ์ ์ด ์๊ณ , ํด๋น ๋
ธ๋์ graph ๊ฐ true ์ด๋ฉด (์น์ฒ๊ด๊ณ์ด๋ฉด) ๋ฐฉ๋ฌธ
dfs(j, p2, depth + 1);
}
}
}
}
ํ์ด (BFS)
dfs ๊ฐ ์ต์ด ์์์ง์ ๋ถํฐ ์ด์ด์ง ๋ ธ๋๋ฅผ ๋๊น์ง ์ฌ๊ท์ ์ผ๋ก ํ์ํ๋ฉด์ ์ฐพ๋ ๋ฐฉ์์ด๋ผ๋ฉด, bfs ๋ ์์ ๋ ธ๋์ ์์ ๋ ธ๋๋ค์ ๋ชจ๋ Queue ์ ๋ด์๋ค๊ฐ ์์ฐจ์ ์ผ๋ก ๋ฐฉ๋ฌธํ๋ฉด์ ์ฐพ๋ ๋ฐฉ์์ด๋ค. bfs ํธ์ถ ์ ์์๋ ธ๋์ ํจ๊ป depth ๋ฅผ ์ ๋ฌํ๋ค.
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
public class Main {
static boolean[][] graph;
static boolean[] visited;
static int answer = -1;
static int n, m;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
graph = new boolean[n + 1][n + 1];
visited = new boolean[n + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
int p1 = Integer.parseInt(st.nextToken());
int p2 = Integer.parseInt(st.nextToken());
m = Integer.parseInt(br.readLine());
int x, y;
for (int i = 0; i < m; i++) {
StringTokenizer kinship = new StringTokenizer(br.readLine());
x = Integer.parseInt(kinship.nextToken());
y = Integer.parseInt(kinship.nextToken());
graph[x][y] = true;
graph[y][x] = true;
}
System.out.println(bfs(p1, p2));
}
private static int bfs(int p1, int p2) {
LinkedList<int[]> queue = new LinkedList<>();
queue.add(new int[] {p1, 0});
visited[p1] = true;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int node = current[0];
int depth = current[1];
if (node == p2) {
return depth;
}
for (int i = 1; i <= n; i++) {
if (!visited[i] && graph[node][i]) {
visited[i] = true;
queue.add(new int[] {i, depth + 1});
}
}
}
return answer;
}
}
๋๊ธ๋จ๊ธฐ๊ธฐ