QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#377237#5071. Check Pattern is GoodInfinityNSWA 31ms4192kbC++205.4kb2024-04-05 06:24:562024-04-05 06:24:58

Judging History

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

  • [2024-04-05 06:24:58]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:4192kb
  • [2024-04-05 06:24:56]
  • 提交

answer

#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int,int> pii;

const int mod=998244353;
inline int add(int x,int y){int ret=x+y;if(ret>=mod)ret-=mod;return ret;}
inline int sub(int x,int y){int ret=x-y;if(ret<0)ret+=mod;return ret;}
inline int mul(int x,int y){return ((ll)x*y)%mod;}
inline int step(int base,int pw){int ret=1;while(pw){if(pw&1)ret=mul(ret,base);base=mul(base,base);pw>>=1;}return ret;}
inline int invv(int x){return step(x,mod-2);}


const int maxn=110;
const int maxv=maxn*maxn;

struct edge{
    int a,b,f,c;
};
int inf=1e9;
int rez,level[maxv],start[maxv],sink,source;
vector<edge>e;
vector<int>vect[maxv];
void add_edge(int u,int v,int c){
    if(c==1)rez++;
    vect[u].pb(e.size());
    e.pb({u,v,0,c});
    vect[v].pb(e.size());
    e.pb({v,u,c,c});

    ///printf("%d %d %d | %d \n",u,v,c,rez);
}
void init_mrezu(){
    e.clear();
    rez=0;
    for(int i=source;i<=sink;i++){
        vect[i].clear();
    }
}
int bfs(){
    for(int i=source;i<=sink;i++)level[i]=0;
    level[source]=1;
    queue<int>q;
    q.push(source);
    while(q.size()){
        int x=q.front();
        q.pop();
        for(int i=0;i<vect[x].size();i++){
            int id=vect[x][i];
            if(e[id].f==e[id].c || level[e[id].b])continue;
            level[e[id].b]=level[x]+1;
            q.push(e[id].b);
        }
    }
    return level[sink];
}
int sflow(int x,int flow){
    if(x==sink)return flow;
    for(;start[x]<vect[x].size();start[x]++){
        int id=vect[x][start[x]];
        if(e[id].f<e[id].c && level[x]+1==level[e[id].b]){
            int tmp=sflow(e[id].b,min(flow,e[id].c-e[id].f));
            if(tmp){
                e[id].f+=tmp;
                e[id^1].f-=tmp;
                return tmp;
            }
        }
    }
    return 0;
}
int get_flow(){
    int ret=0;
    while(bfs()){
        for(int i=source;i<=sink;i++)start[i]=0;
        int pom;
        while(pom=sflow(source,inf))ret+=pom;
    }
    return ret;
}


int a[maxn][maxn],n,m,ind[maxn][maxn],b[maxn][maxn];
pii revind[maxv];
void menjaj(int &x){
    if(x==0)x=2;
    else if(x==2)x=0;
}
void labeluj(){
    for(int i=1;i<n;i++){
        for(int j=1;j<m;j++){

            int cnt[3]={0,0,0};
            cnt[a[i][j]]++;cnt[a[i][j+1]]++;
            cnt[a[i+1][j]]++;cnt[a[i+1][j+1]]++;

            if(cnt[0] && cnt[1])b[i][j]=-1;
            else if(cnt[0])b[i][j]=0;
            else if(cnt[2])b[i][j]=2;
            else b[i][j]=1;

            ///printf("%d %d %d %d |",b[i][j],cnt[0],cnt[1],cnt[2]);

        }
        ///printf("\n");
    }
    ///printf("BNIZ\n");
}
int get_color(int x){
    pii pos=revind[x];
    return b[pos.ff][pos.ss];
}
void solve(){

    labeluj();

    int dx[8]={-1,-1,-1,0,0,1,1,1};
    int dy[8]={-1,0,1,-1,1,-1,0,1};

    int indcnt=0;
    for(int i=1;i<n;i++){
        for(int j=1;j<m;j++){
            ind[i][j]=++indcnt;
            revind[indcnt]={i,j};
        }
    }
    source=0;
    sink=indcnt+1;
    init_mrezu();

    for(int i=1;i<n;i++){
        for(int j=1;j<m;j++){

            if(b[i][j]==0){
                add_edge(source,ind[i][j],1);
            }
            if(b[i][j]==1){
                add_edge(source,ind[i][j],1);
                add_edge(ind[i][j],sink,1);
            }
            if(b[i][j]==2){
                add_edge(ind[i][j],sink,1);
            }

            if(b[i][j]==-1 || b[i][j]==2)continue;

            for(int k=0;k<8;k++){
                int idx=i+dx[k];
                int idy=j+dy[k];
                if(idx<1 || idx>=n || idy<1 || idy>=m || b[idx][idy]==-1 || b[idx][idy]==0)continue;

                add_edge(ind[i][j],ind[idx][idy],inf);
            }

        }
    }

    rez-=get_flow();

    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)if(a[i][j]==1)a[i][j]=2;

    vector<int>cand;
    for(int i=source+1;i<sink;i++){
        if(level[i] && get_color(i)!=2)cand.pb(i);
    }

    for(int i=0;i<cand.size();i++){
        pii pos=revind[cand[i]];

        ///printf("%d %d %d POS\n",pos.ff,pos.ss,cand[i]);

        a[pos.ff][pos.ss]=0;
        a[pos.ff][pos.ss+1]=0;
        a[pos.ff+1][pos.ss]=0;
        a[pos.ff+1][pos.ss+1]=0;
    }

}

int main(){

    ///freopen("test.txt","r",stdin);

    int t;
    scanf("%d",&t);
    while(t--){

        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++){
            string s;
            cin>>s;
            for(int j=1;j<=m;j++)
                if(s[j-1]=='?')a[i][j]=1;
                else if(s[j-1]=='W')a[i][j]=0;
                else if(s[j-1]=='B')a[i][j]=2;
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if((i+j)%2){
                    menjaj(a[i][j]);
                }
            }
        }

        solve();

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if((i+j)%2){
                    menjaj(a[i][j]);
                }
            }
        }

        printf("%d\n",rez);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(a[i][j]==0)printf("W");
                else if(a[i][j]==2)printf("B");
            }
            printf("\n");
        }

    }

    return 0;
}

詳細信息

Test #1:

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

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: 31ms
memory: 4192kb

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:

6
WB
BW
WB
BW
BW
WB
BW
WB
BB
3
BW
WB
BW
BW
WB
WB
25
WBBBWWB
WBWWBWB
BWBBWBW
WBWWBWW
WBWBWBB
BWBBBWW
WBWBBWB
BWBWBBW
WBWBBWW
BBWBWBB
10
BWWBWBW
WBBWBWB
BWBBWBB
BBBWBWB
0
B
W
B
B
B
W
W
W
B
W
5
BWWW
WBWB
WWWW
WBWW
BWBW
WWWW
WWWW
WWWW
BWBW
WBWW
6
WBW
WBW
BWB
WBW
WWW
WBW
BWB
WWW
0
W
B
B
W
0
BBBB
WBWB
3
B...

result:

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