-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
69 lines (55 loc) Β· 1.99 KB
/
Solution.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
58
59
60
61
62
63
64
65
66
67
68
69
package BruteForce.swea1247;
import java.io.*;
import java.util.StringTokenizer;
public class Solution {
static int T, N, min;
static Position start, end;
static Position[] people;
static boolean[] visited;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BruteForce/swea1247/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
T = stoi(br.readLine());
for (int t = 1; t <= T; t++) {
N = stoi(br.readLine());
people = new Position[N];
visited = new boolean[N];
min = Integer.MAX_VALUE;
StringTokenizer st = new StringTokenizer(br.readLine());
start = new Position(stoi(st.nextToken()), stoi(st.nextToken()));
end = new Position(stoi(st.nextToken()), stoi(st.nextToken()));
for (int i = 0; i < N; i++) {
people[i] = new Position(stoi(st.nextToken()), stoi(st.nextToken()));
}
dfs(start, 0, 0);
sb.append("#").append(t).append(" ").append(min).append("\n");
}
System.out.println(sb.toString());
}
static void dfs(Position cur, int count, int dist) {
if (dist > min) return;
if (count == N) {
min = Math.min(min, dist + end.getDist(cur));
return;
}
for (int i = 0; i < N; i++) {
if (!visited[i]) {
visited[i] = true;
dfs(people[i], count+1, dist + people[i].getDist(cur));
visited[i] = false;
}
}
}
static int stoi(String s) { return Integer.parseInt(s); }
static class Position {
int x, y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getDist(Position p) {
return Math.abs(this.x - p.x) + Math.abs(this.y - p.y);
}
}
}