QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#411298#5071. Check Pattern is GoodsycqwqTL 7ms31532kbC++144.3kb2024-05-15 11:23:482024-05-15 11:23:50

Judging History

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

  • [2024-05-15 11:23:50]
  • 评测
  • 测评结果:TL
  • 用时:7ms
  • 内存:31532kb
  • [2024-05-15 11:23:48]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
const int maxn=1e6+5,inf=1e15;
struct node
{
    int v,nxt,w;
}e[maxn];
int head[maxn],tot=1;
void add(int x,int y,int w)
{
    e[++tot]=(node){y,head[x],w};
    head[x]=tot;
    e[++tot]=(node){x,head[y],0};
    head[y]=tot;
}
int de[maxn],s,t,n,m,vis[maxn],now[maxn];
int bfs()
{
    int S=s,T=t;
    memset(de,0x3f,sizeof de);
    queue<int> q;
    q.push(S);
    de[S]=0;
    now[S]=head[S];
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=head[x];i;i=e[i].nxt)
        {
            int v=e[i].v,w=e[i].w; 
            // cout<<"###"<<x<<' '<<v<<' '<<w<<'\n';
            if(w&&de[v]>de[x]+1)
            {
                de[v]=de[x]+1;
                now[v]=head[v];
                if(v==T)
                    return 1;
                q.push(v);
            }
        }
    }
    return 0;
}
int dfs(int x,int flow)
{
    if(x==t)
        return flow;
    int res=0;
    for(int i=now[x];i;i=e[i].nxt)
    {
        int v=e[i].v,w=e[i].w;
        now[x]=i;
        // cout<<"###"<<x<<' '<<v<<' '<<w<<'\n';
        if(!w||de[v]!=de[x]+1||flow==res)
            continue;
        int tl=dfs(v,min(w,flow-res));
        e[i].w-=tl,e[i^1].w+=tl;
        res+=tl;
    }
    if(!res)
        de[x]=INT_MAX;
    return res;
}
int cnt,id[105][105],bk[maxn];
char a[105][105];
signed main()
{
    // freopen("matrix.in","r",stdin);
    // freopen("matrix.out","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0); 
    int _;
    cin>>_;
    while(_--)
    {
        memset(head,0,sizeof head);
        memset(bk,0,sizeof bk);
        tot=1;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='B')
                    a[i][j]='1';
                if(a[i][j]=='W')
                    a[i][j]='0';
                if((i+j)&1)
                {
                    if(a[i][j]=='1')
                        a[i][j]='0';
                    else if(a[i][j]=='0')
                        a[i][j]='1';
                }

            }
        s=++cnt,t=++cnt;
        
        // for(int i=1;i<=n;i++)
        // {
        //     for(int j=1;j<=m;j++)
        //         cout<<a[i][j];
        //     cout<<'\n';
        // }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                id[i][j]=++cnt;
        int qwq=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='1')
                    add(s,id[i][j],inf);
                if(a[i][j]=='0')
                    add(id[i][j],t,inf);
                if(a[i][j]=='?')
                {
                    ++qwq;
                    add(s,id[i][j],19260817);
                    add(id[i][j],t,19260817);
                }
            }
        // for(int i=1;i<=n;i++)
        // {
        //     for(int j=1;j<=m;j++)
        //         cout<<a[i][j];
        //     cout<<'\n';
        // }
        int ans=0;
        for(int i=1;i<n;i++)
            for(int j=1;j<m;j++)
            {
                ++ans,++ans;
                ++cnt;
                add(s,cnt,1);
                add(cnt,id[i][j],inf);
                add(cnt,id[i+1][j],inf);
                add(cnt,id[i][j+1],inf);
                add(cnt,id[i+1][j+1],inf);
                ++cnt;
                bk[cnt]=1;
                add(cnt,t,1);
                add(id[i][j],cnt,inf);
                add(id[i+1][j],cnt,inf);
                add(id[i][j+1],cnt,inf);
                add(id[i+1][j+1],cnt,inf);
            }
        // cout<<ans<<'\n';
        while(bfs())
            ans-=dfs(s,inf);
        cout<<ans+qwq*(19260817)<<'\n';
        bfs();
        // for(int i=2;i<=tot;i++)
        //     cout<<e[i].v<<' '<<e[i].w<<'\n';
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                int tt=id[i][j],pd=de[id[i][j]]<=19260817; 
                // cout<<i<<' '<<j<<' '<<pd<<'\n';
                if(pd^(i+j&1))
                    cout<<'B';
                else
                    cout<<'W';
                
            }
            cout<<'\n';
        }
    } 
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 7ms
memory: 31532kb

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
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:

3
BB
BW
WW
WW
BW
WB
BW
WB
BB
2
BW
WB
BW
BW
WW
BW
9
WBBBWBW
BWBBWWW
WBWBWWB
BWWWBWB
BBWBBWB
WWBWWWB
BWBWWBW
WWWWBBW
BBWBBBW
BWWWWWB
6
BWWBWWB
WBBWWWB
BWWBBBW
BBBWBBB
0
B
W
W
B
B
W
W
W
B
B
15
BWWW
WBWW
WWWB
WBBW
BWWB
BWBW
WBWB
BWBW
WBWW
BWBW
8
WBW
WWB
WBW
BWB
WBW
WWW
WBW
BWB
0
W
B
W
B
1
WBWB
WWBB
3
BW...

result: