QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#772587 | #5071. Check Pattern is Good | OIer_kzc# | WA | 52ms | 5688kb | C++20 | 3.5kb | 2024-11-22 20:31:49 | 2024-11-22 20:32:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
char s[105][105];
#define endl '\n'
#define inf 0x3f3f3f3f
#define dbg(x) cerr<<"In Line "<<__LINE__<<' '<<#x<<" = "<<(x)<<endl
int head[20005],id[105][105],level[20005],zz[20005],t,tot=-1,cnt;
struct line{
int to,flow,nxt;
}edge[180005];
bool bfs(){
queue<int> q;
for(int i=0;i<=t;i++) level[i]=0;
q.emplace(0),*level=1;
while(!q.empty()){
int f=q.front();q.pop();
for(int i=head[f];~i;i=edge[i].nxt) if(edge[i].flow&&!level[edge[i].to]){
level[edge[i].to]=level[f]+1,q.emplace(edge[i].to);
}
}
return level[t];
}
int dfs(int u,int flow){
if(u==t) return flow;
int tmp=flow;
for(int &i=zz[u];~i;i=edge[i].nxt) if(level[edge[i].to]==level[u]+1&&edge[i].flow){
int ret=dfs(edge[i].to,min(flow,edge[i].flow));
if(!ret){
level[edge[i].to]=0;continue;
}
tmp-=ret,edge[i].flow-=ret,edge[i^1].flow+=ret;
if(!tmp) return flow;
}
return tmp-flow;
}
inline void add(int u,int v,int w){
edge[++tot]={v,w,head[u]},head[u]=tot;
edge[++tot]={u,0,head[v]},head[v]=tot;
}
bool vis[20005];
pair<int,int> fid[10005];
void dfs(int u){
vis[u]=1;
if(u>cnt){
bool no0=0,no1=0;
for(int x=0;x<2;x++) for(int y=0;y<2;y++){
if(s[fid[u-cnt].first+x][fid[u-cnt].second+y]=='B') no1=1;
if(s[fid[u-cnt].first+x][fid[u-cnt].second+y]=='W') no0=1;
}
if(!no0||!no1) for(int i=0;i<2;i++) for(int j=0;j<2;j++) s[i+fid[u-cnt].first][j+fid[u-cnt].second]='W';
}
for(int i=head[u];~i;i=edge[i].nxt) if(edge[i].flow&&!vis[edge[i].to]) dfs(edge[i].to);
}
int main(){
int T;cin>>T;
while(T--){
int n,m;cin>>n>>m,cnt=0;
tot=-1;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
cin>>s[i][j];
if(i+j&1){
if(s[i][j]=='B') s[i][j]='W';else if(s[i][j]=='W') s[i][j]='B';
}
}
int ans=0;
for(int i=1;i<n;i++) for(int j=1;j<m;j++) id[i][j]=++cnt,fid[cnt]={i,j};
t=cnt<<1|1;
for(int i=0;i<=t;i++) head[i]=-1,vis[i]=0;
for(int i=1;i<n;i++) for(int j=1;j<m;j++){
bool no0=0,no1=0;
for(int x=0;x<2;x++) for(int y=0;y<2;y++){
if(s[i+x][j+y]=='B') no1=1;
if(s[i+x][j+y]=='W') no0=1;
}
if(!no0||!no1){
add(id[i][j],id[i][j]+cnt,1),ans++;
if(no0&&!no1) add(0,id[i][j],inf);
if(no1&&!no0) add(id[i][j],t,inf);
}
}
for(int i=1;i<n;i++) for(int j=1;j<m;j++){
if(i>1) add(id[i][j]+cnt,id[i-1][j],inf),add(id[i-1][j]+cnt,id[i][j],inf);
if(j>1) add(id[i][j]+cnt,id[i][j-1],inf),add(id[i][j-1]+cnt,id[i][j],inf);
if(i>1&&j>1) add(id[i][j]+cnt,id[i-1][j-1],inf),add(id[i-1][j-1]+cnt,id[i][j],inf);
}
while(bfs()){
for(int i=0;i<=t;i++) zz[i]=head[i];
ans-=dfs(0,inf);
}
cout<<ans<<endl,dfs(0);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(s[i][j]=='?') s[i][j]='B';
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(s[i][j]=='B'){
if(i+j&1) cout<<'W';
else cout<<'B';
}
if(s[i][j]=='W'){
if(i+j&1) cout<<'B';
else cout<<'W';
}
}
cout<<endl;
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
3 2 2 ?? ?? 3 3 BW? W?B ?BW 3 3 BW? W?W ?W?
output:
1 BW WB 1 BWB WBB BBW 4 BWB WBW BWB
result:
ok ok (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 52ms
memory: 5688kb
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 9 WBBBWWB WBWBWWW BWBBWWB WBWWBWW BBWBBWB WWBBWWW BWBWBWB WWWWBBW BBWBBWW BBWBWBB 6 BWWBWWB WBBWWWB BWWBBBW BBBWBBB 0 B W B B B W W W B W 14 BWWW WBWB WWWB WBBW BWBW BWBW WBWB BWBW WBWW BWBW 6 WBW WBW BWB WBW WWW WBW BWB WWW 0 W B B W 1 BBWB WWBB 3 BW...
result:
wrong answer There are 13 check pattern in you output, but you answered 14 (test case 6)