QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99038 | #5071. Check Pattern is Good | 1kri | WA | 319ms | 5648kb | C++14 | 2.9kb | 2023-04-21 09:18:49 | 2023-04-21 09:18:53 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 1000000000
using namespace std;
struct FLOW{
int n,m,s,t,u[100005],v[100005],w[100005],first[100005],nxt[100005];
void init(int _s,int _t){
n=_t,m=1,s=_s,t=_t;
memset(first,0,sizeof(first));
memset(nxt,0,sizeof(nxt));
return;
}
void add(int a,int b,int c){
int i=++m;
u[i]=a,v[i]=b,w[i]=c;
nxt[i]=first[u[i]],first[u[i]]=i;
i=++m;
u[i]=b,v[i]=a,w[i]=c;
nxt[i]=first[u[i]],first[u[i]]=i;
return;
}
int q[100005],head,tail,depth[100005],cur[100005];
bool bfs(){
for (int i=1;i<=n;i++)depth[i]=-1,cur[i]=first[i];
depth[s]=0;
head=tail=1;
q[1]=s;
while(head<=tail){
int now=q[head];
head++;
for (int i=first[now];i;i=nxt[i])
if (w[i]>0&&depth[v[i]]==-1){
depth[v[i]]=depth[now]+1;
q[++tail]=v[i];
if (v[i]==t)return 1;
}
}
return 0;
}
int dfs(int now,int in){
if (now==t)return in;
int out=0;
for (int i=cur[now];i!=0&&in>0;i=nxt[i]){
cur[now]=i;
if (depth[v[i]]!=depth[now]+1||w[i]==0)continue;
int qwq=dfs(v[i],min(in,w[i]));
in-=qwq,out+=qwq;
w[i]-=qwq,w[i^1]+=qwq;
}
return out;
}
int work(){
int ans=0;
while(bfs())ans+=dfs(s,inf);
return ans;
}
}flow;
int t,n,m;
char a[105][105];
int id(int x,int y){
return (x-1)*m+y;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%s",a[i]+1);
for (int j=1;j<=m;j++)
if ((i+j)&1){
if (a[i][j]=='B')a[i][j]='W';
else a[i][j]='B';
}
}
int s=2*n*m+1,t=2*n*m+2;
flow.init(s,t);
int ans=0;
for (int i=1;i<n;i++)
for (int j=1;j<m;j++){
int c=0;
if (a[i][j]!='W'&&a[i][j+1]!='W'&&a[i+1][j]!='W'&&a[i+1][j+1]!='W'){
c++;
flow.add(s,id(i,j),1);
}
if (a[i][j]!='B'&&a[i][j+1]!='B'&&a[i+1][j]!='B'&&a[i+1][j+1]!='B'){
c++;
flow.add(n*m+id(i,j),t,1);
}
ans+=c;
if (c>0)flow.add(id(i,j),n*m+id(i,j),c);
}
for (int i=1;i<n;i++)
for (int j=1;j<m;j++){
if (i<n-1){
flow.add(id(i,j)+n*m,id(i+1,j),inf);
flow.add(id(i+1,j)+n*m,id(i,j),inf);
}
if (j<m-1){
flow.add(id(i,j)+n*m,id(i,j+1),inf);
flow.add(id(i,j+1)+n*m,id(i,j),inf);
}
}
printf("%d\n",ans-flow.work());
for (int i=1;i<=flow.m;i++){
if (flow.u[i]==s&&flow.w[i]==0){
int x=flow.v[i]/m+1,y=flow.v[i]%m;
if (y==0)x--,y=m;
a[x][y]=a[x][y+1]=a[x+1][y]=a[x+1][y+1]='B';
}
if (flow.v[i]==t&&flow.w[i]==0){
int x=(flow.u[i]-n*m)/m+1,y=(flow.u[i]-n*m)%m;
if (y==0)x--,y=m;
a[x][y]=a[x][y+1]=a[x+1][y]=a[x+1][y+1]='W';
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (a[i][j]=='?')a[i][j]='B';
if ((i+j)&1){
if (a[i][j]=='B')a[i][j]='W';
else a[i][j]='B';
}
printf("%c",a[i][j]);
}
printf("\n");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4384kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 1 BWB WWB BBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 319ms
memory: 5648kb
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 7 WBBBWWB WBWBWWW BWBBWWB WBWWBWW BBWBBWB WWBBWWW BWBWBWB WWWWBBW BBWBBWW BBWBWBB 4 BWWBWWB WBBWWWB BWWBBBB BBBWBBB 0 B W B B B W W W B W 5 BWWW WBWB WWWW WBWW BWBW WWWW WWWW WWWW BWBW WBWW 6 WBW WBW BWB WBW WWW WBW BWB WWW 0 W B B W 0 BBBB WBWB 3 BWB...
result:
wrong answer There are 9 check pattern in you output, but you answered 7 (test case 3)