-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMain.java
More file actions
253 lines (241 loc) · 8.1 KB
/
Main.java
File metadata and controls
253 lines (241 loc) · 8.1 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Solver practice2_d = new practice2_d();
static Solver arc074_d = new arc074_d();
static Solver abc193_f = new abc193_f();
public static void main(String[] args) {
// practice2_d.run();
// arc074_d.run();
abc193_f.run();
}
}
abstract class Solver implements Runnable { public abstract void run(); }
/**
* @problem
* AtCoder Library Practice Contest D - Maxflow
* {@see https://atcoder.jp/contests/practice2/tasks/practice2_d}
* @submission
* {@see https://atcoder.jp/contests/practice2/submissions/20808482}
*/
class practice2_d extends Solver {
public Answer solve(int n, int m, char[][] g) {
MaxFlow mf = new MaxFlow(n * m + 2);
int s = n * m;
int t = s + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == '#') continue;
if (((i ^ j) & 1) == 0) {
mf.addEdge(s, i * m + j, 1);
} else {
mf.addEdge(i * m + j, t, 1);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = i & 1; j < m; j += 2) {
if (g[i][j] == '#') continue;
if (j - 1 >= 0 && g[i][j - 1] == '.') {
mf.addEdge(i * m + j, i * m + (j - 1), 1);
}
if (j + 1 < m && g[i][j + 1] == '.') {
mf.addEdge(i * m + j, i * m + (j + 1), 1);
}
if (i - 1 >= 0 && g[i - 1][j] == '.') {
mf.addEdge(i * m + j, (i - 1) * m + j, 1);
}
if (i + 1 < n && g[i + 1][j] == '.') {
mf.addEdge(i * m + j, (i + 1) * m + j, 1);
}
}
}
int ans = (int) mf.maxFlow(s, t);
int cnt = 0;
for (var e : mf.getEdges()) {
if (e.flow == 0) continue;
int u = e.from;
int v = e.to;
if (u == s || v == t) continue;
int ui = u / m, uj = u % m;
int vi = v / m, vj = v % m;
if (g[ui][uj] != '.' || g[vi][vj] != '.') {
throw new AssertionError();
}
if (ui == vi) {
if (uj + 1 == vj) {
g[ui][uj] = '>';
g[vi][vj] = '<';
} else if (uj == vj + 1) {
g[ui][uj] = '<';
g[vi][vj] = '>';
} else {
throw new AssertionError();
}
} else if (uj == vj) {
if (ui + 1 == vi) {
g[ui][uj] = 'v';
g[vi][vj] = '^';
} else if (ui == vi + 1) {
g[ui][uj] = '^';
g[vi][vj] = 'v';
} else {
throw new AssertionError();
}
} else {
throw new AssertionError();
}
cnt++;
}
if (ans != cnt) throw new AssertionError();
return new Answer(ans, g);
}
static class Answer {
final int c;
final char[][] g;
Answer(int c, char[][] g) { this.c = c; this.g = g; }
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(c);
for (char[] row : g) {
sb.append('\n').append(row);
}
return sb.toString();
}
}
public void run() {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
int m = Integer.parseInt(sc.next());
char[][] g = new char[n][];
Arrays.setAll(g, i -> sc.next().toCharArray());
sc.close();
System.out.println(solve(n, m, g));
}
}
/**
* @problem
* AtCoder Regular Contest 074 F - Lotus Leaves
* {@see https://atcoder.jp/contests/arc074/tasks/arc074_d}
* @submission
* {@see https://atcoder.jp/contests/arc074/submissions/20808863}
*/
class arc074_d extends Solver {
static final int INF = 1 << 20;
public int solve(int h, int w, char[][] g) {
int si = -1, sj = -1, ti = -1, tj = -1;
boolean[][] grid = new boolean[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (g[i][j] == 'S') {
si = i; sj = j;
grid[i][j] = true;
} else if (g[i][j] == 'T') {
ti = i; tj = j;
grid[i][j] = true;
} else if (g[i][j] == 'o') {
grid[i][j] = true;
}
}
}
int s = h + w;
int t = s + 1;
MaxFlow mf = new MaxFlow(h + w + 2);
mf.addEdge(s , si , INF);
mf.addEdge(s , sj + h, INF);
mf.addEdge(ti , t , INF);
mf.addEdge(tj + h, t , INF);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (grid[i][j]) {
mf.addEdge(i, j + h, 1);
mf.addEdge(j + h, i, 1);
}
}
}
int flow = (int) mf.maxFlow(s, t);
return flow >= INF ? -1 : flow;
}
public void run() {
Scanner sc = new Scanner(System.in);
int h = Integer.parseInt(sc.next());
int w = Integer.parseInt(sc.next());
char[][] g = new char[h][];
Arrays.setAll(g, i -> sc.next().toCharArray());
sc.close();
System.out.println(solve(h, w, g));
}
}
/**
* @problem
* AtCoder Beginner Contest 193 F - Zebraness
* {@see https://atcoder.jp/contests/abc193/tasks/abc193_f}
* @submission
* {@see https://atcoder.jp/contests/abc193/submissions/20808965}
*/
class abc193_f extends Solver {
static final int[] dx4 = {1, 0, -1, 0};
static final int[] dy4 = {0, 1, 0, -1};
public int solve(int n, char[][] c) {
int[][] id = new int[n][n];
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (c[i][j] == '?') {
id[i][j] = cnt++;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < n - 1) {
if (c[i][j] == 'B' && c[i + 1][j] == 'W' || c[i][j] == 'W' && c[i + 1][j] == 'B') {
ans++;
}
}
if (j < n - 1) {
if (c[i][j] == 'B' && c[i][j + 1] == 'W' || c[i][j] == 'W' && c[i][j + 1] == 'B') {
ans++;
}
}
}
}
MaxFlow mf = new MaxFlow(cnt + 2);
int s = cnt, t = cnt + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (c[i][j] == '?') {
int b = 0, w = 0;
for (int d = 0; d < 4; d++) {
int ni = i + dx4[d];
int nj = j + dy4[d];
if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;
if (c[ni][nj] == 'B') {
b++;
} else if (c[ni][nj] == 'W') {
w++;
} else if (d < 2) {
mf.addEdge(id[i][j], id[ni][nj], 1);
mf.addEdge(id[ni][nj], id[i][j], 1);
ans++;
}
}
mf.addEdge(id[i][j], t, (i + j) % 2 == 1 ? b : w);
mf.addEdge(s, id[i][j], (i + j) % 2 == 1 ? w : b);
ans += b + w;
}
}
}
return (int) (ans - mf.maxFlow(s, t));
}
public void run() {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
char[][] c = new char[n][];
Arrays.setAll(c, i -> sc.next().toCharArray());
sc.close();
System.out.println(solve(n, c));
}
}