QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411678 | #5071. Check Pattern is Good | _LSA_ | WA | 38ms | 3796kb | C++14 | 3.7kb | 2024-05-15 17:38:23 | 2024-05-15 17:38:23 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define mk make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define eb emplace_back
using namespace std;
ll read(){
ll X = 0,r = 1;
char ch = getchar();
while(!isdigit(ch) && ch != '-') ch = getchar();
if(ch == '-') r = -1,ch = getchar();
while(isdigit(ch)) X = X*10+ch-'0',ch = getchar();
return X*r;
}
const int N = 110;
const int dx[9] = {0,0,0,1,-1,1,-1,1,-1};
const int dy[9] = {0,1,-1,0,0,1,-1,-1,1};
const int inf = 1e9;
int n,m;
int mp[N][N];
int black[N][N],white[N][N],cnt,S,T;
bool chk1(int x,int y){
if(mp[x][y] == 0 || mp[x+1][y] == 0 || mp[x][y+1] == 0 || mp[x+1][y+1] == 0) return false;
return true;
}
bool chk2(int x,int y){
if(mp[x][y] == 1 || mp[x+1][y] == 1 || mp[x][y+1] == 1 || mp[x+1][y+1] == 1) return false;
return true;
}
const int M = 2e6+10;
int head[M],nxt[M],to[M],edge[M],tot;
void add_edge(int u,int v,int w){
// cout << u << " " << v << " " << w << "\n";
to[++tot] = v; nxt[tot] = head[u];
head[u] = tot; edge[tot] = w;
to[++tot] = u; nxt[tot] = head[v];
head[v] = tot; edge[tot] = 0;
}
int dep[M],now[M];
bool bfs(){
for(int i=0;i<=cnt;i++) dep[i] = 0;
queue<int> q;
q.push(S);
now[S] = head[S]; dep[S] = 1;
while(!q.empty()){
int x = q.front(); q.pop();
for(int i=head[x];i;i=nxt[i]){
int y = to[i];
if(dep[y] || !edge[i]) continue;
dep[y] = dep[x]+1;
now[y] = head[y];
if(y == T) return true;
q.push(y);
}
}
return false;
}
int dinic(int x,int flow){
if(x == T) return flow;
int rest = flow;
for(int i=now[x];i && rest;i=nxt[i]){
now[x] = i;
int y = to[i];
if(dep[y] != dep[x]+1 || !edge[i]) continue;
int k = dinic(y,min(rest,edge[i]));
if(!k) dep[y] = 0;
edge[i] -= k;
rest -= k;
edge[i^1] += k;
}
return flow-rest;
}
int main(){
int TTT = read();
while(TTT--){
n = read(); m = read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
char ch; cin >> ch;
if(ch == 'W') mp[i][j] = 0;
else if(ch == 'B') mp[i][j] = 1;
else mp[i][j] = 2;
if(((i+j) & 1) && mp[i][j] != 2) mp[i][j] ^= 1;
}
S = 0; T = 1; cnt = 1;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) black[i][j] = white[i][j] = 0;
for(int i=1;i<n;i++)
for(int j=1;j<m;j++){
if(chk1(i,j)) black[i][j] = ++cnt;
if(chk2(i,j)) white[i][j] = ++cnt;
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++) cout << white[i][j] << " ";
// cout << "\n";
// }
for(int i=0;i<=cnt;i++) head[i] = 0;
tot = 1;
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
if(chk1(i,j)) add_edge(S,black[i][j],1);
if(chk2(i,j)) add_edge(white[i][j],T,1);
if(!black[i][j]) continue;
for(int k=0;k<9;k++){
int x = i+dx[k],y = j+dy[k];
if(!white[x][y]) continue;
add_edge(black[i][j],white[x][y],inf);
}
}
}
int flow = 0;
while(bfs()) flow += dinic(S,inf);
cout << cnt-1-flow << "\n";
for(int i=1;i<n;i++)
for(int j=1;j<m;j++) if(black[i][j]){
int x = black[i][j];
for(int I=head[x];I;I=nxt[I]){
int y = to[I];
if(y != S || edge[I]) continue;
mp[i][j] = mp[i+1][j] = mp[i][j+1] = mp[i+1][j+1] = 1;
}
}
for(int i=1;i<n;i++)
for(int j=1;j<m;j++) if(white[i][j]){
int x = white[i][j];
for(int I=head[x];I;I=nxt[I]){
int y = to[I];
if(y != T || !edge[I]) continue;
mp[i][j] = mp[i+1][j] = mp[i][j+1] = mp[i+1][j+1] = 0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(mp[i][j] == 2) mp[i][j] = 0;
if((i+j) & 1) mp[i][j] = mp[i][j]^1;
if(mp[i][j]) putchar('B');
else putchar('W');
}
cout << "\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3760kb
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: 38ms
memory: 3796kb
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 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 BW...
result:
wrong answer There are 3 check pattern in you output, but you answered 5 (test case 12)