QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#519855#5071. Check Pattern is Goodhztmax0RE 1ms3844kbC++144.4kb2024-08-15 08:23:222024-08-15 08:23:22

Judging History

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

  • [2024-08-15 08:23:22]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3844kb
  • [2024-08-15 08:23:22]
  • 提交

answer

#include <iostream>
#include <vector>
#include <queue>
#include <cassert>

using namespace std;

const int Inf = 1e9;

namespace Dinic {
  const int N = 1e4 + 5;

  struct E {
    int v, w, id;
  }; 

  int n, s, t;
  int dis[N];
  vector<E> e[N];
  vector<E>::iterator now[N];

  void Init (int _n, int _s, int _t) {
    n = _n, s = _s, t = _t;
    fill(e + 1, e + n + 1, vector<E>());
  }

  bool Bfs () {
    fill(dis, dis + n + 1, -1), dis[s] = 0; 
    queue<int> q;
    for (q.push(s); !q.empty(); q.pop()) {
      int u = q.front();
      for (auto i : e[u]) {
        int v = i.v, w = i.w;
        if (w && dis[v] == -1) {
          dis[v] = dis[u] + 1;
          q.push(v);
        }
      }
    }
    return dis[t] != -1;
  }

  int Dfs (int u, int d) {
    if (u == t) return d;
    int res = 0; 
    for (auto i = now[u]; i != e[u].end(); ++i) {
      now[u] = i; 
      int v = i->v, &w = i->w, id = i->id;
      if (dis[v] == dis[u] + 1 && w) {
        int r = Dfs(v, min(w, d));
        if (!r) dis[v] = -1;
        res += r;
        d -= r;
        w -= r;
        e[v][id].w += r;
      }
    } 
    return res;
  }

  int Max_flow () {
    int res = 0; 
    while (Bfs()) {
      for (int i = 1; i <= n; ++i) {  
        now[i] = e[i].begin();
      }
      res += Dfs(s, Inf);
    }
    return res;
  }

  void Add_edge (int u, int v, int w) {
    e[u].push_back((E){v, w, int(e[v].size())});
    e[v].push_back((E){u, 0, int(e[u].size()) - 1});
  }
}

namespace _01_Limitation {
  void Init (int n) {
    Dinic::Init(n + 2, n + 1, n + 2);
  }

  void Lim1 (int x, int o, int w) {
    if (!o) {
      Dinic::Add_edge(x, Dinic::t, w);
    } 
    else {
      Dinic::Add_edge(Dinic::s, x, w);
    }
  } 

  void Lim2 (int x, int a, int y, int b, int w) {
    assert(a ^ b == 1); 
    if (a && !b) swap(x, y);
    Dinic::Add_edge(x, y, w);
  }

  pair<int, vector<bool>> Solve () {
    int res = Dinic::Max_flow();
    vector<bool> vec(Dinic::n - 2); 
    for (int i = 0; i < vec.size(); ++i) {
      vec[i] = Dinic::dis[i + 1] == -1;
    }
    return {res, vec};
  }
}

const int N = 105;

int n, m;
int a[N][N]; 

int Id (int x, int y, int o) { return (x - 1) * (m - 1) + y + o * (n - 1) * (m - 1); }

int main () {
  cin.tie(0)->sync_with_stdio(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    _01_Limitation::Init((n - 1) * (m - 1) * 2);
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        char c;
        cin >> c;
        if (c == 'W') 
          a[i][j] = 0;
        else if (c == 'B')
          a[i][j] = 1; 
        else 
          a[i][j] = -1;
      }
    }
    for (int i = 1; i < n; ++i) {
      for (int j = 1; j < m; ++j) {
        int v = (i ^ j) & 1; 
        _01_Limitation::Lim2(Id(i, j, 0), v ^ 1, Id(i, j, 1), v, Inf);
        _01_Limitation::Lim1(Id(i, j, 0), v, 1);
        _01_Limitation::Lim1(Id(i, j, 1), v ^ 1, 1);
        if (j + 1 < m) {
          _01_Limitation::Lim2(Id(i, j, 0), v ^ 1, Id(i, j + 1, 0), v, Inf);
          _01_Limitation::Lim2(Id(i, j, 1), v, Id(i, j + 1, 1), v ^ 1, Inf);
        }
        if (i + 1 < n) {
          _01_Limitation::Lim2(Id(i, j, 0), v ^ 1, Id(i + 1, j, 0), v, Inf);
          _01_Limitation::Lim2(Id(i, j, 1), v, Id(i + 1, j, 1), v ^ 1, Inf);
        }
        if (i + 1 < n && j + 1 < m) {
          _01_Limitation::Lim2(Id(i, j, 0), v ^ 1, Id(i + 1, j + 1, 1), v, Inf);
          _01_Limitation::Lim2(Id(i, j, 1), v, Id(i + 1, j + 1, 1), v ^ 1, Inf);
        }
        if (a[i][j] == 1 || a[i][j + 1] == 0 || a[i + 1][j] == 0 || a[i + 1][j + 1] == 1)  
          _01_Limitation::Lim1(Id(i, j, 0), v ^ 1, Inf);
        if (a[i][j] == 0 || a[i][j + 1] == 1 || a[i + 1][j] == 1 || a[i + 1][j + 1] == 0) 
          _01_Limitation::Lim1(Id(i, j, 1), v, Inf);
      }
    }
    pair<int, vector<bool>> ans = _01_Limitation::Solve();
    for (int o = 0; o < 2; ++o) {
      for (int i = 1; i < n; ++i) {
        for (int j = 1; j < m; ++j) {
          if (ans.second[Id(i, j, o) - 1] ^ o ^ ((i ^ j) & 1)) {
            a[i][j] = o, a[i + 1][j] = o ^ 1, a[i][j + 1] = o ^ 1, a[i + 1][j + 1] = o;
          }
        }
      }
    }
    cout << (n - 1) * (m - 1) * 2 - ans.first << '\n';
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        cout << (a[i][j] == 1 ? 'B' : 'W');
      }
      cout << '\n';
    }
  }
  return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Runtime Error

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:


result: