QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#270934 | #5071. Check Pattern is Good | liangbowen | WA | 66ms | 5704kb | C++14 | 3.7kb | 2023-12-01 18:16:37 | 2023-12-01 18:16:38 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define EMPTY(arr) for (int i = 0; i <= 2 * n * m + 5; i++) arr[i] = 0
using namespace std;
const int N = 114514, inf = 0x3f3f3f3f, dict[9][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
struct Edge {int now, nxt, u, w;} e[1919810];
int head[N], _head[N], cur = 1;
void ad(int u, int v, int w) {e[++cur].now = v, e[cur].nxt = head[u], e[cur].w = w, head[u] = cur;}
void add(int u, int v, int w) {ad(u, v, w), ad(v, u, 0);}
int n, m, s, t; char a[105][105];
void chg(char &x) {x = (x == 'B' ? 'W' : x == 'W' ? 'B' : '?');}
bool black(char v) {return v != 'W';}
bool white(char v) {return v != 'B';}
int black(int x, int y) {return (x - 1) * m + y;}
int white(int x, int y) {return (x - 1) * m + y + n * m;}
bool all_black(int x, int y) {return black(a[x][y]) && black(a[x + 1][y]) && black(a[x][y + 1]) && black(a[x + 1][y + 1]);}
bool all_white(int x, int y) {return white(a[x][y]) && white(a[x + 1][y]) && white(a[x][y + 1]) && white(a[x + 1][y + 1]);}
pair <int, int> toxy(int u) {return (u % m == 0 ? (pair <int, int>){u / m, m} : (pair <int, int>){u / m + 1, u % m});}
namespace Flow {
int dis[N]; bool vis[N];
bool bfs()
{
queue <int> q; EMPTY(vis);
q.push(s), vis[s] = true, dis[s] = 0, _head[s] = head[s];
while (!q.empty())
{
int u = q.front(); q.pop();
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].now;
if (vis[v] || !e[i].w) continue;
vis[v] = true, dis[v] = dis[u] + 1, q.push(v), _head[v] = head[v];
if (v == t) return true;
}
}
return false;
}
int dfs(int u, int maxflow)
{
if (u == t) return maxflow;
int flow = 0;
for (int i = _head[u]; i && flow < maxflow; i = e[i].nxt)
{
_head[u] = i; int v = e[i].now;
if (dis[v] != dis[u] + 1 || !e[i].w) continue;
int ww = dfs(v, min(e[i].w, maxflow - flow));
if (!ww) dis[v] = -inf;
e[i].w -= ww, e[i ^ 1].w += ww, flow += ww;
}
return flow;
}
int dinic()
{
int ans = 0;
while (bfs()) ans += dfs(s, inf);
return ans;
}
}
bool flow[N];
void dfs(int u)
{
flow[u] = true;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].now;
if (!flow[v] && e[i].w) dfs(v);
}
}
void solve()
{
cin >> n >> m, s = 0, t = 2 * n * m + 1;
cur = 1; EMPTY(head); EMPTY(flow);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{cin >> a[i][j]; if ((i + j) & 1) chg(a[i][j]);}
int tot = 0;
for (int x = 1; x < n; x++)
for (int y = 1; y < m; y++)
{
//s 连 Black,t 连 White
if (all_black(x, y)) add(s, black(x, y), 1), tot++;
if (all_white(x, y)) add(white(x, y), t, 1), tot++;
for (int i = 0; i < 9; i++)
{
int dx = x + dict[i][0], dy = y + dict[i][1];
if (1 <= dx && dx < n && 1 <= dy && dy < m) add(black(x, y), white(dx, dy), inf);
}
}
cout << tot - Flow::dinic() << '\n'; //总数-最小割
dfs(s);
for (int i = 2; i <= cur; i += 2)
{
int u = e[i].u, v = e[i].now;
if (!e[i ^ 1].w) //两者状态相同,说明中间的边没被割掉
{
if (u == s) {auto [x, y] = toxy(v); if (all_black(x, y)) a[x][y] = a[x + 1][y] = a[x][y + 1] = a[x + 1][y + 1] = 'B';}
if (v == t) {auto [x, y] = toxy(u - n * m); if (all_white(x, y)) a[x][y] = a[x + 1][y] = a[x][y + 1] = a[x + 1][y + 1] = 'W';}
}
}
for (int i = 1; i <= n; i++, cout << '\n')
for (int j = 1; j <= m; j++)
{
a[i][j] = (a[i][j] == '?' ? 'W' : a[i][j]);
if ((i + j) & 1) chg(a[i][j]);
cout << a[i][j];
}
}
int main()
{
//ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5704kb
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
Wrong Answer
time: 66ms
memory: 5696kb
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:
wrong answer There are 4 check pattern in you output, but you answered 5 (test case 12)