-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
107 lines (85 loc) Β· 2.87 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package Simulation.swea1767;
import java.io.*;
import java.util.*;
class Solution {
static int T, N, core, ans;
static int[][] map;
static ArrayList<int[]> list;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/swea1767/sample_input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
list = new ArrayList<>();
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 1 && i != 0 && j != 0 && i != N-1 && j != N-1) {
list.add(new int[] {i, j});
}
}
}
core = 0;
ans = 0;
dfs(0, 0, 0);
sb.append("#").append(t).append(" ").append(ans).append("\n");
}
System.out.println(sb.toString());
}
static void dfs(int idx, int cnt, int dist) {
if (idx == list.size()) {
if (core < cnt) {
core = cnt;
ans = dist;
} else if (core == cnt) {
ans = Math.min(ans, dist);
}
return;
}
int i = list.get(idx)[0], j = list.get(idx)[1];
for (int k = 0; k < 4; k++) {
int res = find(i, j, k);
if (res == 0) continue;
dfs(idx+1, cnt+1, dist+res);
back(i, j, k);
}
dfs(idx+1, cnt, dist);
}
static void back(int i, int j, int d) {
int to_i = i + di[d], to_j = j + dj[d];
while (isValidPath(to_i, to_j) && map[to_i][to_j] == 2) {
map[to_i][to_j] = 0;
to_i += di[d];
to_j += dj[d];
}
}
static int find(int i, int j, int d) {
int dist = 0;
int to_i = i + di[d], to_j = j + dj[d];
while (isValidPath(to_i, to_j) && map[to_i][to_j] == 0) {
map[to_i][to_j] = 2;
dist ++;
if (to_i == 0 || to_j == 0 || to_i == N-1 || to_j == N-1) return dist;
to_i += di[d];
to_j += dj[d];
}
to_i -= di[d];
to_j -= dj[d];
while (true) {
if (to_i == i && to_j == j) break;
map[to_i][to_j] = 0;
to_i -= di[d];
to_j -= dj[d];
}
return 0;
}
static boolean isValidPath(int i, int j) {
return 0 <= i && i < N && 0 <= j && j < N;
}
}