QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99100#5071. Check Pattern is Good1kriWA 1082ms13896kbC++143.9kb2023-04-21 10:47:552023-04-21 10:47:59

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-21 10:47:59]
  • 评测
  • 测评结果:WA
  • 用时:1082ms
  • 内存:13896kb
  • [2023-04-21 10:47:55]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 1000000000
using namespace std;
struct FLOW{
	int n,m,s,t,u[300005],v[300005],w[300005],first[300005],nxt[300005];
	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]=0;
		nxt[i]=first[u[i]],first[u[i]]=i;
		return;
	}
	int q[300005],head,tail,depth[300005],cur[300005];
	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;
	}
	int tot,a[100005],b[100005];
	int col[100005];
	void qwq_dfs(int now){
		col[now]=1;
		for (int i=first[now];i;i=nxt[i])
			if (w[i]>0&&col[v[i]]==0)qwq_dfs(v[i]);
		return;
	}
	void qwq(){
		tot=0;
		memset(col,0,sizeof(col));
		qwq_dfs(s);
		for (int i=2;i<=m;i+=2){
			if (col[u[i]]==1&&col[v[i]]==0){
				++tot;
				a[tot]=u[i],b[tot]=v[i];
			}
		}
		return;
	}
}flow;
int testcase,n,m;
char a[105][105];
int book0[105][105],book1[105][105];
int id(int x,int y){
	return (x-1)*m+y;
}
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int main(){
	scanf("%d",&testcase);
	while(testcase--){
		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 if (a[i][j]=='W')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++){
				for (int k=0;k<8;k++){
					int x=i+dir[k][0],y=i+dir[k][1];
					if (x<1||x>n-1||y<1||y>m-1)continue;
					flow.add(id(i,j)+n*m,id(x,y),inf);
				}
			}
		ans-=flow.work();
		printf("%d\n",ans);
		flow.qwq();
		memset(book0,0,sizeof(book0));
		memset(book1,0,sizeof(book1));
		for (int i=1;i<=flow.tot;i++){
			if (flow.a[i]==s){
				int x=flow.b[i]/m+1,y=flow.b[i]%m;
				if (y==0)x--,y=m;
				book0[x][y]=1;
			}
			if (flow.b[i]==t){
				int x=(flow.a[i]-n*m)/m+1,y=(flow.a[i]-n*m)%m;
				if (y==0)x--,y=m;
				book1[x][y]=1;
			}
			if (flow.b[i]==flow.a[i]+n*m){
				int x=flow.a[i]/m+1,y=flow.a[i]%m;
				if (y==0)x--,y=m;
				book0[x][y]=book1[x][y]=1;
			}
		}
		for (int i=1;i<n;i++)
			for (int j=1;j<m;j++){
				if (book0[i][j]==0&&a[i][j]!='W'&&a[i][j+1]!='W'&&a[i+1][j]!='W'&&a[i+1][j+1]!='W')
					a[i][j]=a[i][j+1]=a[i+1][j]=a[i+1][j+1]='B';
				if (book1[i][j]==0&&a[i][j]!='B'&&a[i][j+1]!='B'&&a[i+1][j]!='B'&&a[i+1][j+1]!='B')
					a[i][j]=a[i][j+1]=a[i+1][j]=a[i+1][j+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 if (a[i][j]=='W')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: 13896kb

input:

3
2 2
??
??
3 3
BW?
W?B
?BW
3 3
BW?
W?W
?W?

output:

1
WB
BW
1
BWB
WWB
BBW
4
BWB
WBW
BWB

result:

ok ok (3 test cases)

Test #2:

score: -100
Wrong Answer
time: 1082ms
memory: 12280kb

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
11
WBBBWWB
BWBBWWW
WBWBWWB
BWWWBWW
BBWBBWB
WWBWWWB
BWBWBBW
WWWWBBW
BBWBBWW
BBWBWBB
5
BWWBWWB
WBBWWWB
BWWBBBW
BBBWBBB
0
B
W
B
B
B
W
W
W
B
W
14
BWWW
WBWB
WWWB
WBBW
BWWB
BWBW
WBWB
BWBW
WBWW
BWBW
10
WBW
WWB
WBW
BWB
WBW
WBW
BWB
WWW
0
W
B
B
W
1
BBWB
WWBB
3
...

result:

wrong answer There are 9 check pattern in you output, but you answered 11 (test case 3)