QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#277753#5071. Check Pattern is GoodRedreamMerWA 19ms5856kbC++144.6kb2023-12-06 22:19:442023-12-06 22:19:44

Judging History

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

  • [2023-12-06 22:19:44]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:5856kb
  • [2023-12-06 22:19:44]
  • 提交

answer

// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define PB emplace_back
// #define int long long
#define ll long long
#define vi vector<int>
#define siz(a) ((int) ((a).size()))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
void print(vi n) {rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1];}

struct FLOW {
	static const int N = 2e5, inf = 1e9;
	int head[N + 5], S, T, dep[N + 5], cur[N + 5], top;
	struct road {
		int lst, v, val;
	} s[N + 5];
	void init(int n) {
		S = n + 1, T = n + 2;
		rep(i, 1, T) head[i] = 0;
		top = 1;
	}
	inline void add(int l1, int l2, int l3) {
		s[++top] = (road) {head[l1], l2, l3};
		head[l1] = top;
		s[++top] = (road) {head[l2], l1, 0};
		head[l2] = top;
	}
	inline bool bfs() {
		rep(i, 0, T) dep[i] = 0, cur[i] = head[i];
		queue<int> qu;
		qu.push(S);
		dep[S] = 1;
		while (!qu.empty()) {
			int u = qu.front();
			qu.pop();
			for (int i = head[u]; i; i = s[i].lst) {
				int v = s[i].v;
				if (dep[v] || !s[i].val) continue;
				dep[v] = dep[u] + 1;
				qu.push(v);
			}
		}
		return dep[T];
	}
	inline int dfs(int n, int mx) {
		if (n == T) return mx;
		int res = 0;
		for (int i = cur[n]; i; i = s[i].lst) {
			cur[n] = i;
			int v = s[i].v;
			if (dep[v] != dep[n] + 1 || !s[i].val) continue;
			int tmp = dfs(v, min(mx, s[i].val));
			s[i].val -= tmp;
			s[i ^ 1].val += tmp;
			res += tmp;
			mx -= tmp;
			if (!mx) break;
		}
		if (!res) dep[n] = -1;
		return res;
	}
	inline int dinic() {
		int res = 0;
		while (bfs()) res += dfs(S, inf);
		return res;
	}
} S;

const int N = 100;
int a, T, b, s[N + 5][N + 5];
char c[N + 5];

signed main() {
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	for(cin >> T; T--; ) {
		cin >> a >> b;
		S.init((a - 1) * (b - 1) * 2 + a * b);
		auto id = [&] (int x, int y, int z) {
			if(z == 0) return (x - 1) * b + y;
			else if(z == 1) return (x - 1) * (b - 1) + y + a * b;
			else return (x - 1) * (b - 1) + y + a * b + (a - 1) * (b - 1);
		};
		rep(i, 1, a) {
			cin >> c + 1;
			rep(j, 1, b) {
				if(c[j] == 'W') s[i][j] = 0  ^ ((i + j) & 1);
				else if(c[j] == 'B') s[i][j] = 1 ^ ((i + j) & 1);
				else s[i][j] = 2;
				if(s[i][j] == 0) S.add(S.S, id(i, j, 0), 1e9);
				else if(s[i][j] == 1) S.add(id(i, j, 0), S.T, 1e9);
			}
		}
		// rep(i, 1, a) rep(j, 1, b) cout << s[i][j] << " \n"[j == b];
		int cnt = 0;
		rep(i, 1, a - 1) rep(j, 1, b - 1) {
			bool vis[3] = {0, 0, 0};
			// S.add(id(i, j, 1), id(i, j, 2), 1e9);
			vis[s[i][j]] = vis[s[i + 1][j]] = vis[s[i][j + 1]] = vis[s[i + 1][j + 1]] = 1;
			if(!vis[0]) {
				S.add(id(i, j, 2), S.T, 1);
				S.add(id(i, j, 0), id(i, j, 2), 1e9);
				S.add(id(i, j + 1, 0), id(i, j, 2), 1e9);
				S.add(id(i + 1, j, 0), id(i, j, 2), 1e9);
				S.add(id(i + 1, j + 1, 0), id(i, j, 2), 1e9);
			}
			if(!vis[1]) {
				S.add(S.S, id(i, j, 1), 1);
				S.add(id(i, j, 1), id(i, j, 0), 1e9);
				S.add(id(i, j, 1), id(i, j + 1, 0), 1e9);
				S.add(id(i, j, 1), id(i + 1, j, 0), 1e9);
				S.add(id(i, j, 1), id(i + 1, j + 1, 0), 1e9);
			}
		}
		// cerr << S.dinic() << "#@!@#@" << endl;
		cnt = 0;
		rep(i, 1, a - 1) rep(j, 1, b - 1) {
			bool vis[3] = {0, 0, 0};
			vis[s[i][j]] = vis[s[i + 1][j]] = vis[s[i][j + 1]] = vis[s[i + 1][j + 1]] = 1;
			bool ok = 0;
			if(!vis[0]) {
				for(int k = S.head[id(i, j, 2)]; k; k = S.s[k].lst) {
					int v = S.s[k].v, val = S.s[k].val;
					// if(v == S.T) cout << val << endl;
					if(v == S.T && val) {
						++cnt;
						assert(s[i][j] != 0);
						assert(s[i + 1][j] != 0);
						assert(s[i][j + 1] != 0);
						assert(s[i + 1][j + 1] != 0);
						s[i][j] = s[i + 1][j] = s[i][j + 1] = s[i + 1][j + 1] = 1;
						ok = 1;
					}
				}
			}
			if(!vis[1] && !ok) {
				for(int k = S.head[id(i, j, 1)]; k; k = S.s[k].lst) {
					int v = S.s[k].v, val = S.s[k].val;
					if(v == S.S && !val) {
						++cnt;
						assert(s[i][j] != 1);
						assert(s[i + 1][j] != 1);
						assert(s[i][j + 1] != 1);
						assert(s[i + 1][j + 1] != 1);
						s[i][j] = s[i + 1][j] = s[i][j + 1] = s[i + 1][j + 1] = 0;
					}
				}
			}
		}
		cout << cnt << '\n';
		rep(i, 1, a) rep(j, 1, b) {
			if(s[i][j] == 2) s[i][j] = 1;
			s[i][j] ^= (i + j) & 1;
			s[i][j] ? cout << 'B' : cout << 'W';
			if(j == b) cout << '\n';
		}
	}
	return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 19ms
memory: 5856kb

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
BWBBWWW
WBWBWWB
BWWWBWW
BBWBBWB
WWBWWWB
BWBWBBW
WWWWBBW
BBWBBWW
BBWBWBB
6
BWWBWWB
WBBWWWB
BWWBBBW
BBBWBBB
0
B
W
B
B
B
W
W
W
B
W
15
BWWW
WBWB
WWWB
WBBW
BWWB
BWBW
WBWB
BWBW
WBWW
BWBW
8
WBW
WWB
WBW
BWB
WBW
WBW
BWB
WWW
0
W
B
B
W
1
BBWB
WWBB
3
BW...

result:

wrong answer ans finds a larger answer (test case 12)