Skip to content

Commit aaa18af

Browse files
committed
[DFS] baekjoon-10971
1 parent f61cbca commit aaa18af

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

β€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| 05 | | [Baekjoon-15658 μ—°μ‚°μž λΌμ›Œλ„£κΈ° (2)](./src/DFS/P15658) | |
2525
| 06 | | [Baekjoon-9095 1, 2, 3 λ”ν•˜κΈ°](./src/DFS/P9095) | |
2626
| 07 | | [Baekjoon-14501 퇴사](./src/DFS/P14501) | |
27+
| 08 | | [Baekjoon-10971 μ™ΈνŒμ› 순회 2](./src/DFS/P10971) | |
2728

2829
### BFS
2930

β€Žsrc/DFS/P10971/Main.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package DFS.P10971;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N;
9+
static int[][] W;
10+
11+
static boolean[] visited;
12+
static int min = Integer.MAX_VALUE;
13+
14+
public static void main(String[] args) throws Exception {
15+
System.setIn(new FileInputStream("src/DFS/P10971/input.txt"));
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
18+
N = Integer.parseInt(br.readLine());
19+
W = new int[N][N];
20+
21+
for (int i = 0; i < N; i++) {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
for (int j = 0; j < N; j++) {
24+
W[i][j] = Integer.parseInt(st.nextToken());
25+
}
26+
}
27+
28+
visited = new boolean[N];
29+
for (int i = 0; i < N; i++) {
30+
solve(i, i, 0, 0);
31+
}
32+
33+
System.out.println(min);
34+
}
35+
36+
static void solve(int start, int from, int count, int sum) {
37+
if (count == N && start == from) {
38+
min = Math.min(min, sum);
39+
return;
40+
}
41+
42+
for (int to = 0; to < N; to++) {
43+
if (!visited[to] && W[from][to] != 0) {
44+
visited[to] = true;
45+
solve(start, to, count + 1, sum + W[from][to]);
46+
visited[to] = false;
47+
}
48+
}
49+
}
50+
}

β€Žsrc/DFS/P10971/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [baekjoon-10971] μ™ΈνŒμ› 순회 2
2+
3+
![image](https://user-images.githubusercontent.com/22045163/94675648-040d0e00-0355-11eb-8ae9-ac6673464fa5.png)

β€Žsrc/DFS/P10971/input.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4
2+
0 10 15 20
3+
5 0 9 10
4+
6 13 0 12
5+
8 8 9 0

0 commit comments

Comments
Β (0)