QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359119 | #5071. Check Pattern is Good | Doqe | WA | 53ms | 3756kb | C++14 | 3.0kb | 2024-03-20 13:17:02 | 2024-03-20 13:17:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace Flow
{
const int N=4e4+10,M=5e5+10;
int hd[N],to[M<<1],nx[M<<1],W[M<<1],tt=1,po;
void add(int u,int v,int w){to[++tt]=v,W[tt]=w,nx[tt]=hd[u],hd[u]=tt;}
void link(int u,int v,int w){add(u,v,w),add(v,u,0);po=max({po,u,v});}
int q[N],l,r,cur[N],dep[N],S,T,n,m;
bool bfs()
{
memset(dep,0,sizeof(int)*(po+10));
memcpy(cur,hd,sizeof(int)*(po+10));
q[l=r=1]=S;dep[S]=1;
while(l<=r)
{
int u=q[l++];
for(int i=hd[u];i;i=nx[i])
{
int v=to[i];
if(W[i]&&!dep[v])q[++r]=v,dep[v]=dep[u]+1;
}
}
return dep[T];
}
int dfs(int u,int f)
{
if(u==T)return f;
int r=0,k;
for(int&i=cur[u];i;i=nx[i])
{
int v=to[i];
if(W[i]&&dep[v]==dep[u]+1)
{
k=dfs(v,min(W[i],f));
f-=k,r+=k;W[i]-=k,W[i^1]+=k;if(!f)break;
}
}
if(f)dep[u]=0;
return r;
}
int Dinic()
{
// cerr<<"FLOW: "<<po<<" "<<S<<" "<<T<<endl;
int flow=0;
while(bfs())flow+=dfs(S,0x3f3f3f3f);
return flow;
}
void ini()
{
memset(hd,0,sizeof(int)*(po+10));
tt=1;po=0;
}
}
const int N=110;
int I[N][N],n,m;
char s[N][N],trans[128];
int main()
{
int T;
cin>>T;
trans['W']='B';
trans['B']='W';
trans['?']='?';
while(T--)
{
cin>>n>>m;Flow::ini();
for(int i=1;i<=n;++i)cin>>s[i]+1;
int cnt=1;Flow::S=1,Flow::T=2;Flow::po=2;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
I[i][j]=(cnt+=2);
cnt+=2;
// for(int i=1;i<n;++i)
// for(int j=1;j<n;++j)
// A[i][j]=(cnt+=2);
int X=0;
for(int i=n;i;--i)
for(int j=m;j;--j)
{
if(i+j&1)s[i][j]=trans[s[i][j]];
if(s[i][j]=='?')
{
Flow::link(Flow::S,I[i][j],1);
Flow::link(I[i][j],I[i][j]+1,100000);
Flow::link(I[i][j]+1,Flow::T,1);++X;
}
}
// for(int i=1;i<=n;++i)cerr<<s[i]+1<<"_\n";
int Q=Flow::hd[Flow::S];
for(int i=1;i<n;++i)
for(int j=1;j<m;++j)
{
// cerr<<"CMP: "<<s[i][j]<<s[i+1][j]<<s[i][j+1]<<s[i+1][j+1]<<endl;
if( s[i][j]!='W'&&s[i+1][j]!='W'&&
s[i][j+1]!='W'&&s[i+1][j+1]!='W')
{
// cerr<<"NEW: S"<<endl;
Flow::link(Flow::S,++cnt,1);
Flow::link(cnt,I[i][j],1);
Flow::link(cnt,I[i][j+1],1);
Flow::link(cnt,I[i+1][j],1);
Flow::link(cnt,I[i+1][j+1],1);
++X;
}
if( s[i][j]!='B'&&s[i+1][j]!='B'&&
s[i][j+1]!='B'&&s[i+1][j+1]!='B')
{
// cerr<<"NEW: T"<<endl;
Flow::link(++cnt,Flow::T,1);
Flow::link(I[i][j]+1,cnt,1);
Flow::link(I[i][j+1]+1,cnt,1);
Flow::link(I[i+1][j]+1,cnt,1);
Flow::link(I[i+1][j+1]+1,cnt,1);
++X;
}
}
// cerr<<"DINIC_______________\n";
int ans=Flow::Dinic();
// cerr<<"ED\n";
// cerr<<X<<" "<<ans<<endl;
cout<<X-ans<<endl;
for(int i=1;i<=n;++i,cout<<"\n")
for(int j=1;j<=m;++j)
{
char c;
if(s[i][j]!='?')c=s[i][j];
else
{
assert(Flow::to[Q]==I[i][j]);
c=!Flow::W[Q]?'B':'W';
Q=Flow::nx[Q];
}
if(i+j&1)c=trans[c];
cout<<c;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3700kb
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: 53ms
memory: 3756kb
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 WB 9 WBBBWWB WBWBWWW BWBBWWB WBWWBWW BBWBBWB WWBBWWW BWBWBWB WWWWBBW BBWBBWW BBWBWBB 6 BWWBWWB WBBWWWB BWBBBBB BBBWBBB 0 B W B B B W W W B W 15 BWWW WBWB WWWW WBWW BWBW WWWW WWWW WWWW BWBW WBWW 8 WBW WBW BWB WBW WWW WBW BWB WWW 0 W B B W 1 BBBB WBWB 3 BW...
result:
wrong answer There are 3 check pattern in you output, but you answered 6 (test case 4)