QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#772758 | #5071. Check Pattern is Good | OIer_kzc | WA | 75ms | 5772kb | C++20 | 3.4kb | 2024-11-22 21:37:45 | 2024-11-22 21:37:46 |
Judging History
answer
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <algorithm>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
constexpr int NR = 105, N = 20005, M = 1000005;
constexpr int INF = 0x3f3f3f3f;
int n, m;
char gr[NR][NR];
int S, T;
int h[N], e[M], ne[M], w[M], idx;
int d[N], cur[N], q[N], hh, tt;
void add(int x, int y) {
ne[++idx] = h[x], h[x] = idx, e[idx] = y, w[idx] = 1;
ne[++idx] = h[y], h[y] = idx, e[idx] = x, w[idx] = 0;
}
bool bfs() {
memset(d, 0, T + 1 << 2);
memcpy(cur, h, T + 1 << 2);
d[S] = 1, *q = S, hh = tt = 0;
while (hh <= tt) {
int x = q[hh++], y;
for (int i = h[x]; i; i = ne[i])
if (!d[y = e[i]] && w[i]) {
d[y] = d[x] + 1;
q[++tt] = y;
}
}
return d[T] > 0;
}
int find(int x, int limit) {
if (x == T) {
return limit;
}
int flow = 0, t;
for (int i = cur[x], y; i && flow < limit; i = ne[i]) {
y = e[i], cur[x] = i;
if (d[y] == d[x] + 1 && w[i]) {
t = find(y, min(limit - flow, w[i]));
if (!t) d[y] = 0;
w[i] -= t, w[i ^ 1] += t, flow += t;
}
}
return flow;
}
int dinic() {
int maxf = 0;
while (bfs()) {
maxf += find(S, INF);
}
return maxf;
}
int id(int x, int y) {
assert(0 < x && x < n && 0 < y && y < m);
return x * m + y;
}
int main() {
int task;
for (scanf("%d", &task); task--; ) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%s", gr[i]);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (gr[i][j] == '?') {
gr[i][j] = -1;
} else {
gr[i][j] = (gr[i][j] == 'B') ^ (i + j & 1);
}
}
}
/* for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
printf("%d ", gr[i][j]);
}
puts("");
} */
S = 0, T = 2 * n * m + 1;
memset(h, 0, T + 1 << 2), idx = 1;
int B = n * m;
for (int x = 1; x < n; ++x) {
for (int y = 1; y < m; ++y) {
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
int tx = x + dx, ty = y + dy;
if (tx < 1 || ty < 1 || tx >= n || ty >= m) {
continue;
}
int s = id(x, y), t = id(tx, ty);
add(s, t + B);
add(s + B, t);
}
}
}
}
int cntv = 0;
for (int x = 1; x < n; ++x) {
for (int y = 1; y < m; ++y) {
int s = id(x, y);
char v[4] = {gr[x][y], gr[x][y - 1], gr[x - 1][y], gr[x - 1][y - 1]};
if (find(v, v + 4, 1) == v + 4) {
// LOG("%d %d S\n", x, y);
add(S, s);
++cntv;
}
if (find(v, v + 4, 0) == v + 4) {
// LOG("%d %d T\n", x, y);
add(s + B, T);
++cntv;
}
}
}
// LOG("%d\n", cntv);
printf("%d\n", cntv - dinic());
for (int i = h[S]; i; i = ne[i]) {
int s = e[i];
if (d[s]) {
int x = s / m, y = s % m;
gr[x - 1][y - 1] = gr[x - 1][y] = gr[x][y - 1] = gr[x][y] = 0;
}
}
for (int i = h[T]; i; i = ne[i]) {
int s = e[i];
if (!d[s]) {
s -= B;
int x = s / m, y = s % m;
gr[x - 1][y - 1] = gr[x - 1][y] = gr[x][y - 1] = gr[x][y] = 1;
}
}
/* for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
printf("%d ", gr[i][j]);
}
puts("");
} */
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
if (gr[x][y] == -1) {
gr[x][y] = 0;
}
if (x + y & 1) {
gr[x][y] ^= 1;
}
putchar("WB"[gr[x][y]]);
}
puts("");
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5772kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 1 BWW WBB WBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 75ms
memory: 5748kb
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 BW 9 WBBBWBW BWBBWWW WBWBWWB BWWWBWB BBWBBWB WWBWWWB BWBWWBW WWWWBBW BBWBBBW BWWWWWB 4 WBWBWWB BBBWWWB WWWBBBW BBBWBBB 0 B W W B B W W W B B 13 WBWW WWWW WWWB BWBW WWWB BWBW WBWB BWBW WBWW BWBW 8 WBW WWB WBW BWB WBW WWW WBW BWB 0 W B W B 1 WBWB WWBB 3 BW...
result:
wrong answer ans finds a larger answer (test case 4)