QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#301128 | #5071. Check Pattern is Good | yllcm | RE | 0ms | 10208kb | C++14 | 3.0kb | 2024-01-09 15:00:34 | 2024-01-09 15:00:34 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define db double
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define FR first
#define SE second
using namespace std;
inline int read() {
int x = 0; bool op = false;
char c = getchar();
while(!isdigit(c))op |= (c == '-'), c = getchar();
while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return op ? -x : x;
}
const int N = 110;
const int M = 1e6 + 10;
const int INF = 1e9;
int n, m, S, T;
char a[N][N], ans[N][N], rev[200];
int id(int x, int y) {return (x - 1) * m + y;}
int etot;
int head[M], to[M], nxt[M], flow[M];
void addedge(int u, int v, int w) {
to[++etot] = v; flow[etot] = w;
nxt[etot] = head[u]; head[u] = etot;
return ;
}
void add(int u, int v, int w) {addedge(u, v, w); addedge(v, u, 0);}
int lev[N], cur[N];
bool bfs() {
for(int i = 0; i <= T; i++)lev[i] = 0, cur[i] = head[i];
lev[S] = 1; queue<int> q; q.push(S);
while(q.empty() == false) {
int u = q.front(); q.pop();
for(int i = head[u]; i; i = nxt[i]) {
if(flow[i] == 0 || lev[to[i]])continue;
lev[to[i]] = lev[u] + 1;
if(to[i] == T)return true;
q.push(to[i]);
}
}
return false;
}
int dinic(int u, int w) {
if(u == T)return w;
int v = w;
for(int i = cur[u]; i && v; i = nxt[i]) {
cur[u] = i;
if(flow[i] == 0 || lev[to[i]] != lev[u] + 1)continue;
int inc = dinic(to[i], min(v, flow[i]));
if(!inc)lev[to[i]] = 0;
flow[i] -= inc; flow[i ^ 1] += inc; v -= inc;
}
return w - v;
}
int vis[N];
void dfs(int u) {
if(vis[u])return ; vis[u] = true;
for(int i = head[u]; i; i = nxt[i])dfs(to[i]);
return ;
}
void solve() {
n = read(); m = read();
rev['W'] = 'B'; rev['B'] = 'W'; rev['?'] = '?';
for(int i = 1; i <= n; i++)scanf("%s", a[i] + 1);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if((i + j) & 1)a[i][j] = rev[a[i][j]];
}
}
S = 0; T = n * m + 1; etot = 1;
for(int i = 0; i <= T; i++)head[i] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(i < n) {
int w = (j > 1 && j < m ? 2 : 1);
add(id(i, j), id(i + 1, j), w);
add(id(i + 1, j), id(i, j), w);
}
if(j < m) {
int w = (i > 1 && i < n ? 2 : 1);
add(id(i, j), id(i, j + 1), w);
add(id(i, j + 1), id(i, j), w);
}
if(a[i][j] == 'B')add(S, id(i, j), INF);
if(a[i][j] == 'W')add(id(i, j), T, INF);
}
}
int cnt = 0, v = 0;
while(bfs())while(v = dinic(S, INF))cnt += v;
assert(~cnt & 1); cnt /= 2;
for(int i = 0; i <= T; i++)vis[i] = false;
dfs(S);
printf("%d\n", (n - 1) * (m - 1) - cnt);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(a[i][j] != '?')ans[i][j] = a[i][j];
else ans[i][j] = vis[id(i, j)] ? 'B' : 'W';
putchar(((i + j) & 1) ? rev[ans[i][j]] : ans[i][j]);
}
putchar('\n');
}
return ;
}
int main() {
int test = read();
while(test--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10208kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 WB BW 1 BWB WBB BBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Runtime Error
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 ...