QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#400759#5071. Check Pattern is GoodtderTL 4ms34868kbC++143.8kb2024-04-27 15:56:032024-04-27 15:56:03

Judging History

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

  • [2024-04-27 15:56:03]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:34868kb
  • [2024-04-27 15:56:03]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5, M = 1e2 + 5, INF = 1e18;
namespace Dinic {
    int n, s, t, cnt = 1, h[N], d[N], v[N], ans, f[N];
    struct Node {
        int u, v, w, h;
    } e[N];
    void make(int u, int v, int w = 1) {
		// cout<<"make "<<u<<" "<<v<<" "<<w<<endl;
        e[++cnt] = {u, v, w, h[u]}; h[u] = cnt;
        e[++cnt] = {v, u, 0, h[v]}; h[v] = cnt;
    }
    bool bfs() {
        for(int i = 1; i <= n; i++) d[i] = INF;
        queue<int> q;
        q.push(s); d[s] = 0; f[s] = h[s];
        while(!q.empty()) {
            int x = q.front(); q.pop();
            for(int i = h[x]; i; i = e[i].h) {
                int p = e[i].v;
                if(e[i].w > 0 && d[p] == INF) {
                    q.push(p);
                    d[p] = d[x] + 1; f[p] = h[p];
                    if(p == t) return 1;
                }
            }
        }
        return 0;
    }
    int dfs(int x, int l) {
        if(x == t) return l;
        int k, r = 0;
        for(int i = f[x]; i && l; i = e[i].h) {
            f[x] = i;
            int p = e[i].v;
            if(e[i].w > 0 && d[p] == d[x] + 1) {
                k = dfs(p, min(l, e[i].w));
                if(!k) d[p] = INF;
                e[i].w -= k; e[i ^ 1].w += k;
                r += k, l -= k;
            }
        }
        return r;
    }
    void init(int nn, int ss, int tt){
        n = nn, s = ss, t = tt;
        cnt = 1;
        memset(h, 0, sizeof(h)); memset(d, 0, sizeof(d)); memset(v, 0, sizeof(v)); memset(f, 0, sizeof(f));
    }
    int dinic() {
        ans = 0;
        while(bfs()) {
            int x;
            while((x = dfs(s, INF))) ans += x;
        }
        return ans;
    }
}
int n, m, s, t, cnt; char a[M][M];
bool check(int x1, int y1, int x2, int y2, char c) {
	return ((a[x1][y1] == c || a[x1][y1] == '?') && (a[x1][y2] == c || a[x1][y2] == '?') && (a[x2][y1] == c || a[x2][y1] == '?') && (a[x2][y2] == c || a[x2][y2] == '?'));
}
int change(int x, int y) {
	return (x - 1) * m + y;
}
int b[N];
void dfs(int u) {
	b[u] = 1;
	for(int i = Dinic::h[u]; i; i = Dinic::e[i].h) {
		int v = Dinic::e[i].v, w = Dinic::e[i].w;
		if(!b[v] && w) dfs(v);
	} 
}
int getx(int k) {
	return (k - 1) / m + 1;
}
int gety(int k) {
	return (k - 1) % m + 1;
}
void setvalue(int x1, int y1, int x2, int y2, char c) {

}
void solve() {
	// s = 'B', t = 'W'
	cnt = 0;
    cin>>n>>m; s = n * m * 2 + 1, t = n * m * 2 + 2; Dinic::init(t, s, t);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin>>a[i][j];
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) 
		if((i + j) % 2 && a[i][j] != '?') a[i][j] = ((a[i][j] == 'W' ? 'B' : 'W'));
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) {
		if(i + 1 > n || j + 1 > m) continue;
		if(check(i, j, i + 1, j + 1, 'B')) Dinic::make(s, change(i, j)), cnt++;
		if(check(i, j, i + 1, j + 1, 'W')) Dinic::make(change(i, j) + n * m, t), cnt++;
		for(int dx = -1; dx <= 1; dx++) for(int dy = -1; dy <= 1; dy++) {
			int nx = i + dx, ny = j + dy;
			if(nx >= 1 && nx <= n && ny >= 1 && ny <= m) Dinic::make(change(i, j), change(nx, ny) + n * m, INF);
		}
	}
	// cout<<"cnt = "<<cnt<<endl;
	cout<<cnt - Dinic::dinic()<<endl;
	dfs(s);
	for(int i = 2; i <= Dinic::cnt; i += 2) {
		int u = Dinic::e[i].u, v = Dinic::e[i].v;
		if(b[u] == b[v])
			if(u == s) {
				int x = getx(v), y = gety(v);
				setvalue(x, y, x + 1, y + 1, 'B');
			} else if(v == t) {
				int x = getx(u - n * m), y = gety(u - n * m);
				setvalue(x, y, x + 1, y + 1, 'W');
			}
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(a[i][j] == '?') a[i][j] = 'B';
			if((i + j) % 2) a[i][j] = ((a[i][j] == 'W' ? 'B' : 'W'));
			cout<<a[i][j];
		}
		cout<<endl;
	}
}
signed main() {
	int t; cin>>t;
	while(t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 34868kb

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
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
WB
9
WBBBWWB
WBWBWWW
BWBBWWB
WBWWBWW
BBWBBWB
WWBBWWW
BWBWBWB
WWWWBBW
BBWBBWW
BBWBWBB
6
BWWBWWB
WBBWWWB
BWBBBBB
BBBWBBB
0
B
W
B
B
B
W
W
W
B
W
15
BWWW
WBWB
WWWW
WBWW
BWBW
WWWW
WWWW
WWWW
BWBW
WBWW
8
WBW
WBW
BWB
WBW
WWW
WBW
BWB
WWW
0
W
B
B
W
1
BBBB
WBWB
3
BW...

result: