QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121526 | #5071. Check Pattern is Good | supepapupu | WA | 133ms | 8200kb | C++17 | 4.0kb | 2023-07-08 13:27:45 | 2023-07-08 13:27:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010, M = 2e6 + 10, INF = 1e8;
int n, m, S, T;
int h[N], e[M], f[M], ne[M], idx;
int q[N], d[N], cur[N];
char g[110][110];
int dx[] = {0, 0, 1, 1}, dy[] = {0, 1, 0, 1};
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}
bool bfs() {
int hh = 0, tt = 0;
memset(d, -1, sizeof d);
q[0] = S, d[S] = 0, cur[S] = h[S];
while (hh <= tt) {
int t = q[hh++];
for (int i = h[t]; ~i; i = ne[i]) {
int ver = e[i];
if (d[ver] == -1 && f[i]) {
d[ver] = d[t] + 1;
cur[ver] = h[ver];
if (ver == T) return 1;
q[ ++ tt] = ver;
}
}
}
return false;
}
int find(int u, int limit) {
if (u == T) return limit;
int flow = 0;
for (int i = cur[u]; ~i && flow < limit; i = ne[i]) {
cur[u] = i;
int ver = e[i];
if (d[ver] == d[u] + 1 && f[i]) {
int t = find(ver, min(f[i], limit - flow));
if (!t) d[ver] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
int dinic() {
int r = 0, flow;
while (bfs()) while (flow = find(S, INF)) r += flow;
return r;
}
int get(int x, int y, int type) {
return x * m + y + type * n * m;
}
bool checkb(int x, int y) {
for (int i = 0; i < 4; ++i) if (g[x + dx[i]][y + dy[i]] == 'W') return 0;
return 1;
}
bool checkw(int x, int y) {
for (int i = 0; i < 4; ++i) if (g[x + dx[i]][y + dy[i]] == 'B') return 0;
return 1;
}
void dfs(int u) {
st[u] = 1;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!st[j] && f[i]) dfs(j);
}
}
void solve() {
scanf("%d%d", &n, &m);
S = N - 2, T = N - 1;
idx = 0;
h[S] = h[T] = -1;
for (int i = 0; i < n; ++i) scanf("%s", g[i]);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (i + j & 1 && g[i][j] != '?') g[i][j] = 'B' + 'W' - g[i][j];
h[get(i, j, 0)] = h[get(i, j, 1)] = -1;
st[get(i, j, 0)] = st[get(i, j, 1)] = 0;
}
int tot = 0;
for (int i = 0; i < n - 1; ++i)
for (int j = 0; j < m - 1; ++j) {
if (checkb(i, j)) add(S, get(i, j, 0), 1), ++tot;
if (checkw(i, j)) add(get(i, j, 1), T, 1), ++tot;
add(get(i, j, 0), get(i, j, 1), INF);
if (i != n - 2) {
add(get(i, j, 0), get(i + 1, j, 1), INF);
add(get(i + 1, j, 0), get(i, j, 1), INF);
if (j != m - 2) {
add(get(i, j, 0), get(i + 1, j + 1, 1), INF);
add(get(i + 1, j + 1, 0), get(i, j, 1), INF);
}
}
if (j != m - 2) {
add(get(i, j, 0), get(i, j + 1, 1), INF);
add(get(i, j + 1, 0), get(i, j, 1), INF);
}
}
printf("%d\n", tot - dinic());
dfs(S);
for (int i = 0; i < idx; i += 2) {
int u = e[i ^ 1], v = e[i];
if (st[u] == st[v])
if (u == S) {
int x = v / m, y = v % m;
for (int j = 0; j < 4; ++j)
g[x + dx[j]][y + dy[j]] = 'B';
}
else if (v == T) {
u -= n * m;
int x = u / m, y = u % m;
for (int j = 0; j < 4; ++j)
g[x + dx[j]][y + dy[j]] = 'W';
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
if (g[i][j] == '?') putchar('B');
else if (i + j & 1) putchar('B' + 'W' - g[i][j]);
else putchar(g[i][j]);
puts("");
}
}
int main() {
int tcase;
scanf("%d", &tcase);
while (tcase--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 8148kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 WB BW 1 BWB WWB BBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 133ms
memory: 8200kb
input:
10000 9 2 BB BW WW WW ?W ?B B? W? BB 6 2 ?? ?B B? BW WW ?? 10 7 WBBBW?? ???BWWW ???BWWB ??WWBW? BBWBBWB WWB?WW? BWBW??? WWWWBBW BBWBB?W B?W?W?B 4 7 ??WBWWB ?BBWWWB ?W?BBB? BBBWBBB 10 1 B W ? B B W W W B ? 10 4 ??WW W?W? WWW? ???W ?W?? ?W?W W?W? ?W?W ???W ???W 8 3 WBW W?? ??? ??? W?W W?W ??? ?W? 4 1 ...
output:
3 BB BW WW WW BW WB BW WB BB 2 BW WB BW BW WW BB 9 WBBBWBB BWBBWWW WBWBWWB BWWWBWB BBWBBWB WWBWWWB BWBWBBW WWWWBBW BBWBBBW BBWBWWB 6 BWWBWWB WBBWWWB BWWBBBW BBBWBBB 0 B W B B B W W W B B 15 BWWW WBWB WWWB WBBW BWWB BWBW WBWB BWBW WBWW BWBW 8 WBW WWB WBW BWB WBW WBW WBW BWB 0 W B B B 1 BBWB WWBB 3 BW...
result:
wrong answer There are 4 check pattern in you output, but you answered 5 (test case 18)