QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#772507 | #5071. Check Pattern is Good | OIer_kzc# | WA | 90ms | 3856kb | C++20 | 3.8kb | 2024-11-22 19:57:24 | 2024-11-22 19:57:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
char s[105][105];
#define endl '\n'
#define inf 0x3f3f3f3f
#define dbg(x) cerr<<"In Line "<<__LINE__<<' '<<#x<<" = "<<(x)<<endl
int head[20005],id[105][105],level[20005],zz[20005],t,tot=-1,cnt;
struct line{
int to,flow,nxt;
}edge[140005];
bool bfs(){
queue<int> q;
for(int i=0;i<=t;i++) level[i]=0;
q.emplace(0),*level=1;
while(!q.empty()){
int f=q.front();q.pop();
for(int i=head[f];~i;i=edge[i].nxt) if(edge[i].flow&&!level[edge[i].to]){
level[edge[i].to]=level[f]+1,q.emplace(edge[i].to);
}
}
return level[t];
}
int dfs(int u,int flow){
if(u==t) return flow;
int tmp=flow;
for(int &i=zz[u];~i;i=edge[i].nxt) if(level[edge[i].to]==level[u]+1&&edge[i].flow){
int ret=dfs(edge[i].to,min(flow,edge[i].flow));
if(!ret){
level[edge[i].to]=0;continue;
}
tmp-=ret,edge[i].flow-=ret,edge[i^1].flow+=ret;
if(!tmp) return flow;
}
return tmp-flow;
}
inline void add(int u,int v,int w){
edge[++tot]={v,w,head[u]},head[u]=tot;
edge[++tot]={u,0,head[v]},head[v]=tot;
}
bool vis[20005];
pair<int,int> fid[10005];
void dfs(int u){
vis[u]=1;
if(u>cnt){
for(int i=0;i<2;i++) for(int j=0;j<2;j++) s[i+fid[u-cnt].first][j+fid[u-cnt].second]='W';
}
for(int i=head[u];~i;i=edge[i].nxt) if(edge[i].flow&&!vis[edge[i].to]) dfs(edge[i].to);
}
int main(){
int T;cin>>T;
while(T--){
int n,m;cin>>n>>m,cnt=0;
tot=-1;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
cin>>s[i][j];
if(i+j&1){
if(s[i][j]=='B') s[i][j]='W';else if(s[i][j]=='W') s[i][j]='B';
}
}
int ans=0;
for(int i=1;i<n;i++) for(int j=1;j<m;j++) id[i][j]=++cnt,fid[cnt]={i,j};
t=cnt<<1|1;
for(int i=0;i<=t;i++) head[i]=-1,vis[i]=0;
for(int i=1;i<n;i++) for(int j=1;j<m;j++){
bool no0=0,no1=0;
for(int x=0;x<2;x++) for(int y=0;y<2;y++){
if(s[i+x][j+y]=='B') no1=1;
if(s[i+x][j+y]=='W') no0=1;
}
if(!no0||!no1){
add(id[i][j],id[i][j]+cnt,1),ans++;
if(no0&&!no1) add(0,id[i][j],inf);
if(no1&&!no0) add(id[i][j],t,inf);
}
else add(id[i][j],id[i][j]+cnt,inf);
}
for(int i=1;i<n;i++) for(int j=1;j<m;j++){
if(i>1) add(id[i][j]+cnt,id[i-1][j],inf),add(id[i-1][j]+cnt,id[i][j],inf);
if(j>1) add(id[i][j]+cnt,id[i][j-1],inf),add(id[i][j-1]+cnt,id[i][j],inf);
}
while(bfs()){
for(int i=0;i<=t;i++) zz[i]=head[i];
ans-=dfs(0,inf);
}
for(int i=1;i<n;i++) for(int j=1;j<m;j++){
bool no0=0,no1=0;
for(int x=0;x<2;x++) for(int y=0;y<2;y++){
if(s[i+x][j+y]=='B') no1=1;
if(s[i+x][j+y]=='W') no0=1;
}
if(!no0||!no1){
int tmp=id[i][j];
for(int k=head[tmp];~k;k=edge[k].nxt) if(edge[k].to==tmp+cnt){
if(edge[k].flow){
for(int x=0;x<2;x++) for(int y=0;y<2;y++) s[i+x][j+y]='B';
}
break;
}
}
}
cout<<ans<<endl,dfs(0);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(s[i][j]=='B'){
if(i+j&1) cout<<'W';
else cout<<'B';
}
if(s[i][j]=='W'){
if(i+j&1) cout<<'B';
else cout<<'W';
}
if(s[i][j]=='?') cout<<'B';
}
cout<<endl;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3528kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 1 BWB WBB BBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 90ms
memory: 3856kb
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 7 WBBBWBB BBWBWWW BWBBWWB BBWWBWB BBWBBWB WWBBWWB BWBWBWB WWWWBBW BBWBBWW BBWBWBB 3 BWWBWBW WBBWBWB BWBBWBW BBBWBWB 0 B W B B B W W W B B 11 BWWW WBWB WWWB WBWW BWBB BWBW WBWB BWBW WBWB BWBW 6 WBW WBB BWB WBW WBW WBW BWB BWB 0 W B B B 1 WBWB BWBW 3 BW...
result:
wrong answer ans finds a larger answer (test case 3)