QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#400928#5070. Check Pattern is BadtderWA 0ms14072kbC++143.8kb2024-04-27 18:24:322024-04-27 18:24:33

Judging History

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

  • [2024-04-27 18:24:33]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:14072kb
  • [2024-04-27 18:24:32]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5, M = 1e2 + 5, INF = 1e18;
int n, m, s, t, cnt; char a[M][M];
void memset(int *_Dst) {
	for(int i = 1; i <= n * m * 2 + 5; i++) _Dst[i] = 0;
}
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) {
        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); memset(d); memset(v); memset(f);
    }
    int dinic() {
        ans = 0;
        while(bfs()) {
            int x;
            while((x = dfs(s, INF))) ans += x;
        }
        return ans;
    }
}
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) {
	a[x1][y1] = a[x1][y2] = a[x2][y1] = a[x2][y2] = c;
}
void print() {
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) cout<<a[i][j];
		cout<<endl;
	}
}
void solve() {
	// s = 'B', t = 'W'
	cnt = 0; memset(b);
    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 - 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');
			}
			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] = 'W';
			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: 0
Wrong Answer
time: 0ms
memory: 14072kb

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:

wrong answer Token parameter [name=yesno] equals to "1", doesn't correspond to pattern "YES|NO" (test case 1)