QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#106976#5070. Check Pattern is BadKostlinWA 30ms3724kbC++145.5kb2023-05-19 22:27:082023-05-19 22:27:10

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-19 22:27:10]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:3724kb
  • [2023-05-19 22:27:08]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define fi first
#define sc second
#define mkp make_pair
#define pii pair<int,int>
typedef long long ll;
const int N=105,M=10005,oo=1e9;
inline int read() {
    int x=0,flag=0;char ch=getchar();
    while(ch<'0'||ch>'9') {flag|=(ch=='-');ch=getchar();}
    while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return flag?-x:x;
}
inline int mx(int x,int y) {return x>y?x:y;}
inline int mn(int x,int y) {return x<y?x:y;}
inline void swp(int &x,int &y) {x^=y^=x^=y;}
inline int as(int x) {return x>0?x:-x;}

int n,m,a[N][N],tot,fr[N][N],D[N][N];
char s[N];
int hd[M<<1],num,qwq[M];
struct node {
    int nxt,to;
}e[M<<2];
#define v e[i].to
inline void adde(int x,int y) {
    e[++num]=(node){hd[x],y};
    hd[x]=num;
}
pii q[M]; int l,r;
inline void sub(int x,int y) {
    if(x>1&&y>1) {
        ++D[x-1][y-1];
        if(D[x-1][y-1]==3) q[++r]=mkp(x-1,y-1);
    }
    if(x>1&&y<m) {
        ++D[x-1][y];
        if(D[x-1][y]==3) q[++r]=mkp(x-1,y);
    }
    if(y>1&&x<n) {
        ++D[x][y-1];
        if(D[x][y-1]==3) q[++r]=mkp(x,y-1);
    }
    if(x<n&&y<m) {
        ++D[x][y];
        if(D[x][y]==3) q[++r]=mkp(x,y);
    }
}
inline void work1(int x,int y) {
    if(a[x][y+1]&&a[x+1][y]&&a[x+1][y+1]) {
        if(a[x][y+1]==a[x+1][y]&&a[x][y+1]!=a[x+1][y+1]) {
            a[x][y]=3-a[x+1][y+1];
            sub(x,y);
        }
        return ;
    }
    if(a[x][y]&&a[x+1][y]&&a[x+1][y+1]) {
        if(a[x][y]==a[x+1][y+1]&&a[x][y]!=a[x+1][y]) {
            a[x][y+1]=3-a[x+1][y];
            sub(x,y+1);
        }
        return ;
    }
    if(a[x][y]&&a[x][y+1]&&a[x+1][y+1]) {
        if(a[x][y]==a[x+1][y+1]&&a[x][y]!=a[x][y+1]) {
            a[x+1][y]=3-a[x][y+1];
            sub(x+1,y);
        }
        return ;
    }
    if(a[x][y]&&a[x][y+1]&&a[x+1][y]) {
        if(a[x][y+1]==a[x+1][y]&&a[x][y+1]!=a[x][y]) {
            a[x+1][y+1]=3-a[x][y];
            sub(x+1,y+1);
        }
        return ;
    }
}
inline void prep() {
    l=1;r=0;
    for(int i=1;i<n;++i)
        for(int j=1;j<m;++j) {
            D[i][j]=(a[i][j]>0)+(a[i][j+1]>0)+(a[i+1][j]>0)+(a[i+1][j+1]>0);
            if(D[i][j]==3) q[++r]=mkp(i,j);
        }
    while(l<=r) {
        pii now=q[l++];
        if(D[now.fi][now.sc]!=3) continue;
        work1(now.fi,now.sc);
    }
}
inline bool chk(int x,int y) {
    return !(a[x][y]==a[x+1][y+1]&&a[x+1][y]==a[x][y+1]&&a[x][y]!=a[x][y+1]);
}
inline void work2(int x,int y) {
    if(a[x][y]&&a[x+1][y]&&a[x][y]!=a[x+1][y]) {
        if(a[x][y]==1) adde(fr[x+1][y+1],fr[x][y+1]),adde(fr[x][y+1]+tot,fr[x+1][y+1]+tot);
        else adde(fr[x+1][y+1]+tot,fr[x][y+1]+tot),adde(fr[x][y+1],fr[x+1][y+1]);
    }
    if(a[x][y]&&a[x+1][y+1]&&a[x][y]==a[x+1][y+1]) {
        if(a[x][y]==1) adde(fr[x+1][y]+tot,fr[x][y+1]),adde(fr[x][y+1]+tot,fr[x+1][y]);
        else adde(fr[x+1][y],fr[x][y+1]+tot),adde(fr[x][y+1],fr[x+1][y]+tot);
    }
    if(a[x][y+1]&&a[x+1][y]&&a[x][y+1]==a[x+1][y]) {
        if(a[x][y+1]==1) adde(fr[x][y]+tot,fr[x+1][y+1]),adde(fr[x+1][y+1]+tot,fr[x][y]);
        else adde(fr[x][y],fr[x+1][y+1]+tot),adde(fr[x+1][y+1],fr[x][y]+tot);
    }
    if(a[x][y+1]&&a[x+1][y+1]&&a[x][y+1]!=a[x+1][y+1]) {
        if(a[x][y+1]==1) adde(fr[x][y]+tot,fr[x+1][y]+tot),adde(fr[x+1][y],fr[x][y]);
        else adde(fr[x][y],fr[x+1][y]),adde(fr[x+1][y]+tot,fr[x][y]);
    }
}
int dfn[M<<1],low[M<<1],st[M<<1],top,col[M<<1],co;
bool inst[M<<1];
void tar(int x) {
    dfn[x]=low[x]=++dfn[0];
    st[++top]=x;inst[x]=1;
    for(int i=hd[x];i;i=e[i].nxt) 
        if(!dfn[v]) {
            tar(v);
            low[x]=mn(low[x],low[v]);
        } else if(inst[v]) low[x]=mn(low[x],dfn[v]);
    if(dfn[x]==low[x]) {
        ++co;
        do {
            col[st[top]]=co;
            inst[st[top]]=0;
        }while(st[top--]!=x);
    }
}
inline void print(int x) {printf("%c",(x==1?'B':'W'));}
inline void Clear() {
    for(int i=1;i<=2*tot;++i) dfn[i]=hd[i]=0;
    dfn[0]=co=num=0;
}
inline void solve() {
    n=read();m=read();
    for(int i=1;i<=n;++i) {
        scanf("%s",s+1);
        for(int j=1;j<=m;++j)
            if(s[j]=='B') a[i][j]=1;
            else if(s[j]=='W') a[i][j]=2;
            else a[i][j]=0;
    }
    prep();
    // for(int i=1;i<=n;++i) {
    //     for(int j=1;j<=m;++j)
    //         if(a[i][j]) print(a[i][j]);
    //         else printf("?");
    //     printf("\n");
    // }
    tot=0;
    for(int i=1;i<=n;++i) {
        if(a[i][1]==0) fr[i][1]=++tot;
        for(int j=2;j<=m;++j)
            if(a[i][j]==0) {
                if(a[i][j-1]==0) fr[i][j]=fr[i][j-1];
                else fr[i][j]=++tot;
            }
    }
    bool flag=1;
    for(int i=1;i<n;++i)
        for(int j=1;j<m;++j) {
            int cnt=(a[i][j]>0)+(a[i+1][j]>0)+(a[i][j+1]>0)+(a[i+1][j+1]>0);
            if(cnt==4) flag&=chk(i,j);
            if(cnt==2) work2(i,j);
        }
    for(int i=1;i<=tot*2;++i) if(!dfn[i]) tar(i);
    for(int i=1;flag&&i<=tot;++i)
        if(col[i]==col[i+tot]) flag=0;
        else qwq[i]=(col[i]<col[i+tot]?1:2);
    if(!flag) {
        printf("NO\n");
    } else {
        printf("YES\n");
        for(int i=1;i<=n;++i) {
            for(int j=1;j<=m;++j)
                if(a[i][j]) print(a[i][j]);
                else print(qwq[fr[i][j]]);
            printf("\n");
        }
    }
    Clear();
}
int main() {
    int TT=read();
    while(TT--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2 2
??
??
3 3
BW?
W?B
?BW
3 3
BW?
W?W
?W?

output:

YES
BB
BB
NO
YES
BWB
WWW
BWB

result:

ok ok (3 test cases)

Test #2:

score: 0
Accepted
time: 30ms
memory: 3704kb

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:

YES
BB
BW
WW
WW
BW
BB
BB
WB
BB
YES
BB
BB
BB
BW
WW
BB
NO
NO
YES
B
W
B
B
B
W
W
W
B
B
YES
BBWW
WBWB
WWWB
WWWW
BWBB
BWBW
WWWW
WWWW
BBBW
BBBW
YES
WBW
WBB
BBB
BBB
WBW
WBW
BBB
BWB
YES
W
B
B
B
YES
BBBB
WBBB
YES
BBBBBB
BBWBWB
YES
WBWBB
YES
BWBBBB
WWBBBB
BBBWBB
WBWWBW
YES
B
YES
BWB
BBB
WBB
BBB
WWB
BBB
BBW
BBB...

result:

ok ok (10000 test cases)

Test #3:

score: -100
Wrong Answer
time: 24ms
memory: 3724kb

input:

10000
9 6
?B?W?W
WWBBWB
?WB?BW
B?W?W?
WW??W?
B???BW
?W?WW?
W?B?B?
?W?BB?
10 1
W
?
?
?
?
?
?
?
B
W
9 4
????
????
W???
?W?B
??WW
?BW?
WW?W
??W?
??W?
3 2
?W
?B
BB
2 7
?W?BWWB
??W???W
9 9
?BW?WWW?W
BW?WBBWWW
W?W????WW
W??WW??WW
W?BWB?B?W
??BB?WWWW
W???WBW?W
WWW???WWW
B?WWWWWW?
8 10
W??BWWW??B
?BWBWBW?BW...

output:

NO
YES
W
B
B
B
B
B
B
B
B
W
YES
BBBB
BBBB
WBBB
WWBB
WWWW
BBWB
WWWW
BBWB
BBWB
YES
BW
BB
BB
YES
BWBBWWB
WWWBBWW
NO
NO
YES
WWB
BWB
BBB
BBW
WWW
YES
BWBWWWBBB
BBBBBWBBB
WBBBBBBBW
WWWWBBBBB
BWBBBWBBW
BWBBWWBWW
BWBBBBBWB
YES
WBWBBWB
BBBBWWW
BWBWWWW
BWWWWBB
BBBBWWB
WBBBWBB
WWWBWWB
WWWWWWB
BWWBBWW
YES
WB
BB
B...

result:

wrong answer ans finds the answer, but out doesn't (test case 4084)