QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#291411 | #5071. Check Pattern is Good | hhoppitree | WA | 203ms | 6212kb | C++14 | 3.5kb | 2023-12-26 15:25:07 | 2023-12-26 15:25:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int tot, head[N * N * 2], val[N * N * 25], to[N * N * 25], nxt[N * N * 25];
void add(int x, int y, int z)
{
nxt[tot] = head[x];
head[x] = tot;
to[tot] = y;
val[tot] = z;
tot++;
return;
}
void addEdge(int x, int y, int z)
{
add(x, y, z);
add(y, x, 0);
return;
}
int dis[N], cur[N], maxFlow;
int bfs(int x, int y)
{
memset(dis, 0x3f, sizeof(dis));
dis[x] = 0;
queue<int> Q;
Q.push(x);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = head[x]; ~i; i = nxt[i]) {
if (val[i] && dis[to[i]] > 1e9) {
dis[to[i]] = dis[x] + 1;
Q.push(to[i]);
}
}
}
if (dis[y] > 1e9) {
return 0;
}
memcpy(cur, head, sizeof(cur));
return 1;
}
void dfs(int x, int y, int now)
{
if (x == y) {
maxFlow += now;
return;
}
for (int &i = cur[x]; ~i && now; i = nxt[i]) {
int v = to[i];
if (dis[v] == dis[x] + 1 && val[i]) {
int tmp = -maxFlow;
dfs(v, y, min(now, val[i]));
tmp += maxFlow;
if (!tmp) {
dis[v] = -1;
}
now -= tmp;
val[i] -= tmp;
val[i ^ 1] += tmp;
}
}
return;
}
signed main()
{
int T;
scanf("%d", &T);
while (T--) {
int n, m;
tot = 0;
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
int now = n * m + 1, s = (n - 1) * (m - 1) * 2 + n * m;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
for (int j = 1; j <= m; ++j) {
if (s[j - 1] != '?' && ((i & 1) ^ (j & 1))) {
s[j - 1] ^= 'W' ^ 'B';
}
if (s[j - 1] != 'B') {
addEdge(0, (i - 1) * m + j + 1, 1);
} else {
addEdge(0, (i - 1) * m + j + 1, 1e9);
}
if (s[j - 1] != 'W') {
addEdge((i - 1) * m + j + 1, 1, 1);
} else {
addEdge((i - 1) * m + j + 1, 1, 1e9);
}
}
if (i >= 2) {
for (int j = 2; j <= m; ++j) {
++now;
addEdge(0, now, 1);
addEdge(now, (i - 1) * m + j + 1, 1e9);
addEdge(now, (i - 1) * m + j, 1e9);
addEdge(now, (i - 2) * m + j + 1, 1e9);
addEdge(now, (i - 2) * m + j, 1e9);
++now;
addEdge(now, 1, 1);
addEdge((i - 1) * m + j + 1, now, 1e9);
addEdge((i - 1) * m + j, now, 1e9);
addEdge((i - 2) * m + j + 1, now, 1e9);
addEdge((i - 2) * m + j, now, 1e9);
}
}
}
maxFlow = 0;
while (bfs(0, 1)) {
dfs(0, 1, 1e9);
}
printf("%d\n", s - maxFlow);
bfs(0, 1);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int f = dis[(i - 1) * m + j + 1] < 1e9 ? 'B' : 'W';
if ((i & 1) ^ (j & 1)) {
f ^= 'W' ^ 'B';
}
putchar(f);
}
puts("");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5928kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 WB BW 1 BWW WWB WBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 203ms
memory: 6212kb
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 81 BBBBWBW WBWBWWW BWBBWWB WBWWBWB BBWBBWB WWBWWWB BWBWWBW WWWWBBW BBWBBBW BWWWWWB 6 BWWBWWB WBBWWWB BWWBBBW BBBWBBB 0 B W W B B W W W B B 15 BWWW WBWW WWWB WBBW BWWB BWBW WBWB BWBW WBWW BWBW 8 WBW WWB WBW BWB WBW WWW WBW BWB 0 W B W B 1 WBWB WWBB 3 B...
result:
wrong answer Integer parameter [name=ans] equals to 81, violates the range [0, 70] (test case 3)