QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#790711 | #5071. Check Pattern is Good | louhao088 | WA | 0ms | 7856kb | C++23 | 2.7kb | 2024-11-28 14:58:25 | 2024-11-28 14:58:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
inline int read(){
char ch=getchar();int x=0;bool f=0;
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=1;
for(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
if(f==1)x=-x;return x;
}
const int maxn=1e5+5,inf=1e6;
int n,m,flag[105][105],ans,T,s,t;
char a[maxn];
int id(int x,int y){return (x-1)*m+y;}
struct Dinic
{
int head[maxn],to[maxn*4],nex[maxn*4],w[maxn*4];
int cnt=1,dis[maxn],cur[maxn],vis[maxn],sum,maxflow;
void add(int x,int y,int z){to[++cnt]=y,w[cnt]=z,nex[cnt]=head[x],head[x]=cnt;
to[++cnt]=x,w[cnt]=0,nex[cnt]=head[y],head[y]=cnt;}
void clear(){
for(int i=1;i<=t;i++)head[i]=cur[i]=vis[i]=0;
cnt=1,maxflow=0;
}
bool bfs()
{
for(int i=1;i<=t;i++)dis[i]=-1;
queue<int>q;
q.push(s);dis[s]=0;
for(int i=1;i<=t;i++)cur[i]=head[i];
while(!q.empty()){
int x=q.front();q.pop();
for(int i=head[x];i;i=nex[i]){
if(dis[to[i]]==-1&&w[i]){q.push(to[i]);dis[to[i]]=dis[x]+1;}
}
}
return dis[t]!=-1;
}
int dfs(int x,int flow)
{
if(x==t)return flow;int sum=0;
for(int i=cur[x];i&&flow;i=nex[i]){cur[x]=i;
if(dis[to[i]]==dis[x]+1&&w[i]){
int num=dfs(to[i],min(flow,w[i]));
flow-=num,w[i]-=num;w[i^1]+=num;sum+=num;
}
}
return sum;
}
void dfs1(int x){
vis[x]=1;
//cout<<x<<endl;
for(int i=head[x];i;i=nex[i])
if(w[i]&&!vis[to[i]])dfs1(to[i]);
}
void work()
{
while(bfs())maxflow+=dfs(s,inf);
dfs1(s);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(vis[id(i,j)]){
flag[i][j]=1;
cout<<i<<" "<<j<<endl;
}
}
}G;
void add(int x,int y,int z){G.add(x,y,z);}
void solve(){
n=read(),m=read();
G.clear();
s=n*m+(n-1)*(m-1)*2+1;t=s+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)flag[i][j]=0;
int sum=(n-1)*(m-1)*2,tot=n*m;
for(int i=1;i<=n;i++){
scanf("%s",a+1);
for(int j=1;j<=m;j++){
if((i+j)%2==0){
if(a[j]=='B')a[j]='W';
else if(a[j]=='W')a[j]='B';
}
if(a[j]=='B')add(s,id(i,j),inf);
if(a[j]=='W')add(id(i,j),t,inf);
if(a[j]=='?')add(id(i,j),t,1),add(s,id(i,j),1),sum++;
}
}
for(int i=1;i<n;i++)
for(int j=1;j<m;j++){
++tot;
add(s,tot,1);
add(tot,id(i,j),inf);
add(tot,id(i+1,j),inf);
add(tot,id(i,j+1),inf);
add(tot,id(i+1,j+1),inf);
++tot;
add(tot,t,1);
add(id(i,j),tot,inf);
add(id(i+1,j),tot,inf);
add(id(i,j+1),tot,inf);
add(id(i+1,j+1),tot,inf);
}
G.work();
ans=sum-G.maxflow;
cout<<ans<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if((i+j)%2==0)flag[i][j]^=1;
if(flag[i][j])putchar('B');
else putchar('W');
}
puts("");
}
}
signed main(){
T=read();
while(T--)solve();
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 7856kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 2 3 3 2 3 3 1 BWB WBB BBW 4 BWB WBW BWB
result:
wrong answer Token parameter [name=g[i]] equals to "3", doesn't correspond to pattern "[BW]{1,100}" (test case 2)