QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371408#5071. Check Pattern is GoodGraphcityTL 1ms3776kbC++203.5kb2024-03-30 10:58:302024-03-30 10:58:30

Judging History

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

  • [2024-03-30 10:58:30]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3776kb
  • [2024-03-30 10:58:30]
  • 提交

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[9]={1,0,-1,0,1,-1,1,-1,0},dy[9]={0,1,0,-1,1,1,-1,-1,0};

int n,m,s,t,vis[Maxn+5],dis[Maxn+5],maxf;
char ch[105][105]; int i1[105][105],i2[105][105];
int v1[105][105],v2[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()
{
    scanf("%d%d",&n,&m); s=t=0;
    For(i,1,n) scanf("%s",ch[i]+1);
    if(n==1 || m==1)
    {
        For(i,1,n) {For(j,1,m) putchar(ch[i][j]=='?'?'W':ch[i][j]); putchar('\n');}
        return;
    }
    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)
    {
        if(ch[i][j]!='W' && ch[i][j+1]!='W' && ch[i+1][j]!='W' && ch[i+1][j+1]!='W')
            v1[i][j]=1; else v1[i][j]=0;
        if(ch[i][j]!='B' && ch[i][j+1]!='B' && ch[i+1][j]!='B' && ch[i+1][j+1]!='B')
            v2[i][j]=1; else v2[i][j]=0;
    }
    For(i,1,n-1) For(j,1,m-1) {if(v1[i][j]) i1[i][j]=++s; if(v2[i][j]) i2[i][j]=++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(v1[i][j]) Addedge(i1[i][j],t,1),maxf--;
        if(v2[i][j]) Addedge(s,i2[i][j],1),maxf--;
        if(v2[i][j]) For(_,0,8)
        {
            int x=i+dx[_],y=j+dy[_]; if(x<1 || x>n || y<1 || y>m) continue;
            if(v1[x][y]) Addedge(i2[i][j],i1[x][y],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(v2[i][j] && vis[i2[i][j]]) ch[i][j]=ch[i][j+1]=ch[i+1][j]=ch[i+1][j+1]='W';
        if(v1[i][j] && !vis[i1[i][j]]) 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) {For(j,1,m) putchar(ch[i][j]); putchar('\n');} Clear();
}

int main()
{
    // freopen("1.in","r",stdin);
    
    int T; scanf("%d",&T);
    while(T--) Solve();
    // cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:


result: