QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#370850 | #5071. Check Pattern is Good | Graphcity | TL | 1ms | 3772kb | C++20 | 3.2kb | 2024-03-29 17:41:28 | 2024-03-29 17:41:29 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=1e6,inf=1e9;
const int dx[8]={1,0,-1,0,1,-1,1,-1},dy[8]={0,1,0,-1,1,1,-1,-1};
int n,m,s,t,vis[Maxn+5],dis[Maxn+5],maxf;
char ch[105][105]; int id[105][105],h[105][105];
struct Node{int frm,to,nxt,w;} Edge[Maxn*2+5];
int tot=1,Head[Maxn+5],cur[Maxn+5];
inline void Addedge(int x,int y,int k)
{
Edge[++tot]=Node{x,y,Head[x],k},Head[x]=tot;
Edge[++tot]=Node{y,x,Head[y],0},Head[y]=tot;
}
inline int bfs()
{
queue<int> q; For(i,1,t) dis[i]=vis[i]=0,cur[i]=Head[i];
dis[s]=0,vis[s]=1,q.push(s);
while(!q.empty())
{
int x=q.front(); q.pop();
for(int i=Head[x];i;i=Edge[i].nxt)
{
int y=Edge[i].to;
if(Edge[i].w && !vis[y])
dis[y]=dis[x]+1,vis[y]=1,q.push(y);
}
} return vis[t];
}
inline int dfs(int x,int flow)
{
if(!flow || x==t) {maxf+=flow; return flow;}
int used=0,res=0;
for(int i=cur[x];i && used<flow;i=Edge[i].nxt)
{
int y=Edge[i].to; cur[x]=i;
if(dis[y]==dis[x]+1 && Edge[i].w)
if(res=dfs(y,min(flow-used,Edge[i].w)))
used+=res,Edge[i].w-=res,Edge[i^1].w+=res;
} return used;
}
inline void dfs(int x)
{
vis[x]=1; for(int i=Head[x];i;i=Edge[i].nxt) if(Edge[i].w)
{int y=Edge[i].to; if(!vis[y]) dfs(y);}
}
inline void Clear() {For(i,1,t) Head[i]=cur[i]=0; tot=1,maxf=0;}
inline void Solve()
{
cin>>n>>m; s=t=0;
For(i,1,n) scanf("%s",ch[i]+1);
For(i,1,n) For(j,1,m) if(i+j&1)
{
if(ch[i][j]=='W') ch[i][j]='B';
else if(ch[i][j]=='B') ch[i][j]='W';
}
For(i,1,n-1) For(j,1,m-1) id[i][j]=++s,++s; ++s,t=s+1;
// For(i,1,n) printf("%s\n",ch[i]+1); printf("\n");
For(i,1,n-1) For(j,1,m-1)
{
if(ch[i][j]!='W' && ch[i][j+1]!='W' && ch[i+1][j]!='W' && ch[i+1][j+1]!='W')
Addedge(id[i][j]+1,t,1),maxf--;
if(ch[i][j]!='B' && ch[i][j+1]!='B' && ch[i+1][j]!='B' && ch[i+1][j+1]!='B')
Addedge(s,id[i][j],1),maxf--;
Addedge(id[i][j],id[i][j]+1,inf);
For(_,0,7)
{
int x=i+dx[_],y=j+dy[_]; if(x<1 || x>n || y<1 || y>m) continue;
Addedge(id[i][j],id[x][y]+1,inf);
}
}
while(bfs()) dfs(s,inf); printf("%d\n",-maxf);
For(i,1,t) vis[i]=0; dfs(s);
For(i,1,n-1) For(j,1,m-1)
{
if(ch[i][j]!='B' && ch[i][j+1]!='B' && ch[i+1][j]!='B' && ch[i+1][j+1]!='B')
if(vis[id[i][j]]) ch[i][j]=ch[i][j+1]=ch[i+1][j]=ch[i+1][j+1]='W';
if(ch[i][j]!='W' && ch[i][j+1]!='W' && ch[i+1][j]!='W' && ch[i+1][j+1]!='W')
if(!vis[id[i][j]+1]) ch[i][j]=ch[i][j+1]=ch[i+1][j]=ch[i+1][j+1]='B';
}
For(i,1,n) For(j,1,m) if(ch[i][j]=='?') ch[i][j]='W';
// For(i,1,n) printf("%s\n",ch[i]+1); printf("\n");
For(i,1,n) For(j,1,m) if(i+j&1)
{
if(ch[i][j]=='W') ch[i][j]='B';
else if(ch[i][j]=='B') ch[i][j]='W';
}
For(i,1,n) printf("%s\n",ch[i]+1); Clear();
}
int main()
{
// freopen("1.in","r",stdin);
int T; cin>>T;
while(T--) Solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3772kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 1 BWW WBB 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:
3 BB BW WW WW BW WB BW WB BB 2 BW WB BW BW WW BW 9 WBBBWBW WBWBWWW BWBBWWB WBWWBWB BBWBBWB WWBBWWB BWBWBWB WWWWBBW BBWBBWW BWWWWBB 5 BWWBWWB WBBWWWB BWWBBBW BBBWBBB 0 B W W B B W W W B B 11 WBWW WWWW WWWB WBWW BWBB BWBW WBWB BWBW WBWW BWBW 7 WBW WWB WBW BWB WBW WWW WBW BWB 0 W B W B 0 WBWB WWBB 3 BW...