QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#272838#5071. Check Pattern is GoodC1942huangjiaxuTL 0ms7812kbC++142.6kb2023-12-02 19:36:572023-12-02 19:36:57

Judging History

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

  • [2023-12-02 19:36:57]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:7812kb
  • [2023-12-02 19:36:57]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=1e6+5;
int Ts,n,m,S,T,ans;
char s[105][105];
int hd[N],cur[N],d[N],to[2*M],nx[2*M],w[2*M],num;
bool vis[N];
int id(int x,int y){
	return (x-1)*m+y;
}
pair<int,int>rid(int x){
	return make_pair((x-1)/m+1,(x-1)%m+1);
}
bool bl(int x,int y){
	return s[x][y]!='W';
}
bool wh(int x,int y){
	return s[x][y]!='B';
}
bool cbl(int x,int y){
	return bl(x,y)&&bl(x+1,y)&&bl(x+1,y+1)&&bl(x,y+1);
}
bool cwh(int x,int y){
	return wh(x,y)&&wh(x+1,y)&&wh(x+1,y+1)&&wh(x,y+1);
}
void chg(char &c){
	if(c=='W')c='B';
	else c='W';
}
void add(int x,int y,int z){
	nx[++num]=hd[x],hd[x]=num,to[num]=y,w[num]=z;
	nx[++num]=hd[y],hd[y]=num,to[num]=x,w[num]=0;
}
bool bfs(){
	queue<int>q;
	for(int i=1;i<=T;++i)d[i]=0;
	q.push(S);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=hd[u],v;v=to[i],i;i=nx[i])if(w[i]&&!d[v]){
			d[v]=d[u]+1;
			if(v==T)return true;
			q.push(v);
		}
	}
	return false;
}
int dinic(int nw,int flow){
	if(nw==T)return flow;
	int rest=flow,k;
	for(int &i=cur[nw];i;i=nx[i])if(w[i]&&d[to[i]]==d[nw]+1){
		k=dinic(to[i],min(rest,w[i]));
		w[i]-=k,w[i^1]+=k,rest-=k;
		if(!k)d[to[i]]=0;
		if(!rest)return flow;
	}
	return flow-rest;
}
int Dinic(){
	int maxflow=0;
	while(bfs()){
		for(int i=1;i<=T;++i)cur[i]=hd[i];
		maxflow+=dinic(S,1e8);
	}
	return maxflow;
}
void dfs(int x){
	if(vis[x])return;
	vis[x]=true;
	for(int i=hd[x],v;v=to[i],i;i=nx[i])if(w[i])dfs(v);
}
void solve(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)scanf("%s",s[i]+1);
	if(Ts>100)return;
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)if((i+j&1)&&s[i][j]!='?')chg(s[i][j]);
	S=2*n*m+1,T=S+1;
	for(int i=1;i<=T;++i)hd[i]=0;
	num=1,ans=0;
	for(int i=1;i<n;++i)for(int j=1;j<m;++j){
		if(cbl(i,j))add(S,id(i,j),1),++ans;
		if(cwh(i,j))add(id(i,j)+n*m,T,1),++ans;
		for(int dx=-1;dx<2;++dx)for(int dy=-1;dy<2;++dy){
			int x=i+dx,y=j+dy;
			if(x>0&&x<n&&y>0&&y<m)add(id(i,j),id(x,y)+n*m,1e6);
		}
	}
	ans-=Dinic();
	printf("%d\n",ans);
	for(int i=1;i<=T;++i)vis[i]=false;
	dfs(S);
	for(int i=2;i<=num;i+=2){
		int u=to[i^1],v=to[i];
		if(vis[u]==vis[v]){
			if(u==S){
				auto [x,y]=rid(v);
				s[x][y]=s[x][y+1]=s[x+1][y+1]=s[x+1][y]='B';
			}
			if(v==T){
				auto [x,y]=rid(u-n*m);
				s[x][y]=s[x][y+1]=s[x+1][y+1]=s[x+1][y]='W';
			}
		}
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(s[i][j]=='?')s[i][j]='W';
			if(i+j&1)chg(s[i][j]);
			putchar(s[i][j]);
		}
		putchar(10);
	}
}
int main(){
	scanf("%d",&Ts);
	while(Ts--)solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 7812kb

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
Time Limit Exceeded

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:


result: