QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#399949#5071. Check Pattern is GoodxlwangWA 36ms3996kbC++144.6kb2024-04-26 20:07:452024-04-26 20:07:45

Judging History

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

  • [2024-04-26 20:07:45]
  • 评测
  • 测评结果:WA
  • 用时:36ms
  • 内存:3996kb
  • [2024-04-26 20:07:45]
  • 提交

answer

#include<bits/stdc++.h>
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
using namespace std;
inline int read(){
	int x=0;
	bool f=0;
	char c=getchar();
	while(!isdigit(c)) f|=(c=='-'),c=getchar();
	while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
const int Maxn=1e2+10,inf=1e4;
namespace FLOW{
    static const int Maxn=3e6+10;
    struct node{
        int nxt,to,d;
    }e[Maxn<<1];
    int head[Maxn],cnt=1;
    inline void adde(int u,int v,int w){++cnt;e[cnt].nxt=head[u];head[u]=cnt;e[cnt].to=v;e[cnt].d=w;}
    inline void add(int u,int v,int w){adde(u,v,w);adde(v,u,0);}
    int vis[Maxn],dis[Maxn],now[Maxn],nod,S,T;
    inline bool bfs(){
        // cerr<<S<<' '<<T<<' '<<nod<<endl;
        fr(i,1,nod) dis[i]=inf,vis[i]=0,now[i]=head[i];
        queue<int> q;dis[S]=0;q.push(S);
        while(!q.empty()){
            int x=q.front();q.pop();vis[x]=0;
            for(register int i=head[x];i;i=e[i].nxt){
                int y;y=e[i].to;if(!e[i].d) continue;
                if(dis[y]>dis[x]+1){
                    dis[y]=dis[x]+1;
                    if(y==T) return true;
                    if(!vis[y]) vis[y]=1,q.push(y);
                }
            }
        }
        return dis[T]!=inf;
    }
    inline int check(int x,int flow){
        if(!flow || x==T) return flow;
        int ans=0;
        for(register int i=now[x];i;i=e[i].nxt){
            int y;y=e[i].to;now[x]=i;if(!flow) break;
            if(!(dis[y]==dis[x]+1) || !e[i].d) continue;
            int k=check(y,min(flow,e[i].d));
            ans+=k;flow-=k;e[i].d-=k,e[i^1].d+=k;
        }
        return ans;
    }
    inline int getans(){
        int ans=0;
        while(bfs()) {
            // cerr<<ans<<endl;
            ans+=check(S,inf);
        }return ans;
    }
    inline void clear(){fr(i,1,nod) head[i]=0;cnt=1;}
}
int n,m;
int nod,S,T;
char c[Maxn][Maxn];
int bl[Maxn][Maxn],wh[Maxn][Maxn];
int vis[Maxn*Maxn*5];
int tx[Maxn*Maxn*2],ty[Maxn*Maxn*2];
inline void clear(){
    fr(i,1,nod) vis[i]=0,tx[i]=0;
    nod=S=T=0;FLOW::clear();
}
inline void add(int u,int v,int w){
    // cout<<u<<' '<<v<<' '<<w<<endl;
    FLOW::add(u,v,w);
}
inline void dfs(int x){
    if(vis[x]) return ;
    vis[x]=1;
    for(register int i=FLOW::head[x];i;i=FLOW::e[i].nxt) if(FLOW::e[i].d) dfs(FLOW::e[i].to);
}
inline bool check(int x,int y){return x>=1 && x<n && y>=1 && y<m;}
inline bool checkB(int x,int y){fr(i,x,x+1) fr(j,y,y+1) if(c[i][j]=='W') return false;return true;}
inline bool checkW(int x,int y){fr(i,x,x+1) fr(j,y,y+1) if(c[i][j]=='B') return false;return true;}
int dx[]={-1,-1,-1,0,0,0,1,1,1};
int dy[]={-1,0,1,-1,0,1,-1,0,1};
inline void work(){
    clear();
    n=read();m=read();
    S=1;nod=T=2;
    fr(i,1,n-1) fr(j,1,m-1) bl[i][j]=++nod,tx[nod]=i,ty[nod]=j,wh[i][j]=++nod;
    fr(i,1,n) scanf("%s",c[i]+1);
    fr(i,1,n) fr(j,1,m){
        if((i+j)&1) continue;
        if(c[i][j]=='W') c[i][j]='B';
        else if(c[i][j]=='B') c[i][j]='W';
    }
    int ans=0;
    fr(i,1,n-1) fr(j,1,m-1){
        if(checkW(i,j)) add(S,wh[i][j],1),++ans;
        if(checkB(i,j)) add(bl[i][j],T,1),++ans;
        fr(p,0,8){
            int nx=i+dx[p],ny=j+dy[p];
            if(check(nx,ny)) add(wh[i][j],bl[nx][ny],inf);
        }
    }
    FLOW::S=S;FLOW::T=T;FLOW::nod=nod;
    // cerr<<"**"<<endl;
    ans-=FLOW::getans();writeln(ans);
    dfs(S);
    fr(i,1,nod){
        if(!vis[i] || !tx[i]) continue;
        int x,y;x=tx[i],y=ty[i];
        fr(nx,x,x+1) fr(ny,y,y+1) c[nx][ny]='W';
    }
    fr(i,1,n) fr(j,1,m) if(c[i][j]=='?') c[i][j]='B';
    fr(i,1,n) fr(j,1,m){
        if((i+j)&1) continue;
        if(c[i][j]=='W') c[i][j]='B';
        else if(c[i][j]=='B') c[i][j]='W';
    }
    fr(i,1,n){
        fr(j,1,m) putchar(c[i][j]);
        puts("");
    }
}
inline void init(){
	int t=read();
	while(t--) work();
}
signed main(){
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
    init();
    // printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3996kb

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
Wrong Answer
time: 36ms
memory: 3812kb

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
WB
BW
WB
BW
WB
BW
2
BW
WB
BW
WB
WW
BW
9
WBBBWBW
BWBBWWW
WBWBWWB
BWWWBWB
BBWBBWB
WWBWWWB
BWBWWBW
WWWWBBW
BBWBBBW
BWWWWWB
6
BWBBWWB
WBWWWWB
BWBBBBW
WBWWBBB
0
B
W
W
B
B
W
W
W
B
B
15
BWBW
WBWW
BWBB
WBWW
BWBB
WBWW
WBWB
BWBW
WBWW
BWBW
8
WBW
WWB
WBW
BWB
WBW
WWW
WBW
BWB
0
W
B
W
B
1
WBWB
WWBB
3
BW...

result:

wrong answer (3, 1) should be W, you output B (test case 1)