Skip to content

Commit 022ac0e

Browse files
committed
[BruteForce] swea-1247
1 parent 1d26150 commit 022ac0e

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
| 04 | | [Baekjoon-3040 백설 공주와 일곱 난쟁이](./src/BruteForce/P3040) | |
6565
| 05 | | [Baekjoon-14889 스타트와 링크](./src/BruteForce/P14889) | 삼성 SW 역량 테스트 기출 |
6666
| 06 | | [Baekjoon-16637 괄호 추가하기](./src/BruteForce/P16637) | 삼성 A형 기출 |
67+
| 07 | | [SWEA-1247 최적 경로](./src/BruteForce/swea1247) | |
6768

6869
## DFS & BFS
6970

src/BruteForce/swea1247/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## [SW Expert Academy - 1247] 최적 경로
2+
3+
![image](https://user-images.githubusercontent.com/22045163/108326561-bca4f280-720d-11eb-98af-2da95f3c42b3.png)
4+
![image](https://user-images.githubusercontent.com/22045163/108326630-cd556880-720d-11eb-9e73-146f69667ff1.png)
5+
6+
![image](https://user-images.githubusercontent.com/22045163/108326410-8bc4bd80-720d-11eb-9144-75cce182cc4e.png)

src/BruteForce/swea1247/Solution.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package BruteForce.swea1247;
2+
3+
import java.io.*;
4+
import java.util.StringTokenizer;
5+
6+
public class Solution {
7+
8+
static int T, N, min;
9+
static Position start, end;
10+
static Position[] people;
11+
static boolean[] visited;
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/BruteForce/swea1247/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringBuilder sb = new StringBuilder();
17+
18+
T = stoi(br.readLine());
19+
for (int t = 1; t <= T; t++) {
20+
N = stoi(br.readLine());
21+
people = new Position[N];
22+
visited = new boolean[N];
23+
min = Integer.MAX_VALUE;
24+
25+
StringTokenizer st = new StringTokenizer(br.readLine());
26+
start = new Position(stoi(st.nextToken()), stoi(st.nextToken()));
27+
end = new Position(stoi(st.nextToken()), stoi(st.nextToken()));
28+
for (int i = 0; i < N; i++) {
29+
people[i] = new Position(stoi(st.nextToken()), stoi(st.nextToken()));
30+
}
31+
32+
dfs(start, 0, 0);
33+
34+
sb.append("#").append(t).append(" ").append(min).append("\n");
35+
}
36+
System.out.println(sb.toString());
37+
}
38+
39+
static void dfs(Position cur, int count, int dist) {
40+
if (dist > min) return;
41+
if (count == N) {
42+
min = Math.min(min, dist + end.getDist(cur));
43+
return;
44+
}
45+
46+
for (int i = 0; i < N; i++) {
47+
if (!visited[i]) {
48+
visited[i] = true;
49+
dfs(people[i], count+1, dist + people[i].getDist(cur));
50+
visited[i] = false;
51+
}
52+
}
53+
}
54+
55+
static int stoi(String s) { return Integer.parseInt(s); }
56+
57+
static class Position {
58+
int x, y;
59+
60+
public Position(int x, int y) {
61+
this.x = x;
62+
this.y = y;
63+
}
64+
65+
public int getDist(Position p) {
66+
return Math.abs(this.x - p.x) + Math.abs(this.y - p.y);
67+
}
68+
}
69+
}

src/BruteForce/swea1247/input.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
10
2+
5
3+
0 0 100 100 70 40 30 10 10 5 90 70 50 20
4+
6
5+
88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14
6+
7
7+
22 47 72 42 61 93 8 31 72 54 0 64 26 71 93 87 84 83
8+
8
9+
30 20 43 14 58 5 91 51 55 87 40 91 14 55 28 80 75 24 74 63
10+
9
11+
3 9 100 100 16 52 18 19 35 67 42 29 47 68 59 38 68 81 80 37 94 92
12+
10
13+
39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36
14+
10
15+
26 100 72 2 71 100 29 48 74 51 27 0 58 0 35 2 43 47 50 49 44 100 66 96
16+
10
17+
46 25 16 6 48 82 80 21 49 34 60 25 93 90 26 96 12 100 44 69 28 15 57 63
18+
10
19+
94 83 72 42 43 36 59 44 52 57 34 49 65 79 14 20 41 9 0 39 100 94 53 3
20+
10
21+
32 79 0 0 69 58 100 31 67 67 58 66 83 22 44 24 68 3 76 85 63 87 7 86

src/BruteForce/swea1247/output.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#1 200
2+
#2 304
3+
#3 265
4+
#4 307
5+
#5 306
6+
#6 366
7+
#7 256
8+
#8 399
9+
#9 343
10+
#10 391

0 commit comments

Comments
 (0)