QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#359990 | #5071. Check Pattern is Good | Slongod | WA | 32ms | 3608kb | C++14 | 4.5kb | 2024-03-21 08:44:07 | 2024-03-21 08:44:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
namespace Slongod{
const int N = 5e4+7 , inf = 0x3f3f3f3f;
namespace wll
{
int econt , s , t , head[N] , hu[N] , dep[N];
struct EDGE{int to , nxt , w;}edge[N*10];
inline void _add(int x , int y , int w){edge[++econt].to = y; edge[econt].nxt = head[x]; head[x] = econt; edge[econt].w = w;}
inline void add(int x , int y , int w){_add(x , y , w); _add(y , x , 0);}
inline bool bfs()
{
for (int i = 0; i <= max(s,t); i++){dep[i] = -1; hu[i] = head[i];}
queue <int> q; q.push(s); dep[s] = 0;
while(!q.empty()) {
int u = q.front(); q.pop();
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].to , w = edge[i].w;
if (w and dep[v] == -1) {
dep[v] = dep[u] + 1; q.push(v);
}
}
} return dep[t] != -1;
}
int dfs(int u , int f)
{
if (u == t){return f;} int used = 0;
for (int &i = hu[u]; i; i = edge[i].nxt) {
int v = edge[i].to , w = edge[i].w;
if (dep[v] == dep[u] + 1 and w) {
int tmp = dfs(v , min(f-used , w));
edge[i].w -= tmp; edge[i^1].w += tmp; used += tmp;
if (f == used){break;}
}
} if (!used){dep[u] = -1;} return used;
}
inline int dinic(){int re = 0; while(bfs()){re += dfs(s , inf);} return re;}
inline void init(int S , int T){s = S; t = T; for (int i = S; i <= T; i++){head[i] = 0;} econt = 1;}
}
int n , m , s , t , ans , flag[N] , output[105][105];
const int dx[4] = {-1 , -1 , 0 , 0};
const int dy[4] = {-1 , 0 , -1 , 0};
#define id(x , y) ((x - 1) * (m - 1) + y)
#define rid(x , y , opt) (id(x , y) * 2 - opt)
inline bool cmp(int x , int y){return 1 <= x and x < n and 1 <= y and y < m;}
void main()
{
int T; cin >> T;
while(T--) {
cin >> n >> m; ans = (n - 1) * (m - 1) * 2;
s = 0; t = rid(n-1 , m-1 , 0) + 1;
wll::init(s , t);
for (int i = 1; i <= id(n-1,m-1); i++){flag[i] = 0;}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char a; cin >> a;
if (((i + j) & 1) and a == 'W'){a = 'B';}
else if (((i + j) & 1) and a == 'B'){a = 'W';}
if (a != '?'){output[i][j] = (a == 'W' ? 1 : 2);}
else{output[i][j] = 0;}
for (int k = 0; k < 4; k++) {
int tx = i + dx[k] , ty = j + dy[k];
if (!cmp(tx , ty)){continue;}
if (a == 'W'){flag[id(tx,ty)] |= 1;}
if (a == 'B'){flag[id(tx,ty)] |= 2;}
}
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
wll::add(rid(i,j,1) , rid(i,j,0) , inf);
if (flag[id(i,j)] == 0) {
wll::add(s , rid(i,j,1) , 1);
wll::add(rid(i,j,0) , t , 1);
} else if (flag[id(i,j)] == 1) {
ans--;
wll::add(s , rid(i,j,1) , 1);
} else if (flag[id(i,j)] == 2) {
ans--;
wll::add(rid(i,j,0) , t , inf);
} else {
ans -= 2;
}
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (flag[id(i,j)] == 3){continue;}
if (i > 1) {
for (int k = j; k <= j+1 and k < m; k++) {
if (flag[id(i-1,k)] == 3){continue;}
wll::add(rid(i,j,1) , rid(i-1,k,0) , inf);
wll::add(rid(i-1,k,1) , rid(i,j,0) , inf);
}
}
if (j > 1) {
for (int k = i; k <= i+1 and k < n; k++) {
if (flag[id(k,j-1)] == 3){continue;}
wll::add(rid(i,j,1) , rid(k,j-1,0) , inf);
wll::add(rid(k,j-1,1) , rid(i,j,0) , inf);
}
}
if (i > 1 and j > 1 and flag[id(i-1,j-1)] != 3) {
wll::add(rid(i,j,1) , rid(i-1,j-1,0) , inf);
wll::add(rid(i-1,j-1,1) , rid(i,j,0) , inf);
}
}
}
cout << ans - wll::dinic() << '\n';
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (wll::dep[rid(i,j,1)] != -1 and !(flag[id(i,j)]&2)) {
for (int tx = i; tx <= i+1; tx++) {
for (int ty = j; ty <= j+1; ty++) {
output[tx][ty] = 1;
}
}
}
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (wll::dep[rid(i,j,1)] == -1 and !(flag[id(i,j)]&1)) {
for (int tx = i; tx <= i+1; tx++) {
for (int ty = j; ty <= j+1; ty++) {
output[tx][ty] = 2;
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (((i+j)&1) and output[i][j] == 1){output[i][j] = 2;}
else if (((i+j)&1) and output[i][j] == 2){output[i][j] = 1;}
if (output[i][j] == 1){cout << 'W';}
else{cout << 'B';}
} cout << '\n';
}
}
}
}int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in" , "r" , stdin);
#endif
ios :: sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
return Slongod :: main(),0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3608kb
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: 32ms
memory: 3592kb
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 BB 9 WBBBWBB WBWBWWW BWBBWWB WBWWBWB BBWBBWB WWBBWWB BWBWBWB WWWWBBW BBWBBWW BBWBWBB 6 BWWBWWB WBBWWWB BWWBBBW BBBWBBB 0 B W B B B W W W B B 13 BWWW WBWB WWWB WBWW BWBB BWBW WBWB BWBW WBWW BWBW 6 WBW WBW BWB WBW WBW WBW BWB BWB 0 W B B B 1 BBWB WWBB 3 BW...
result:
wrong answer ans finds a larger answer (test case 6)