Descroption
Original link you have a \ (n*m\) rectangle, at the beginning all the squares are white, and then give a target state of the rectangle, some place is white, some place is black, you can choose a connecting block each time (four connected blocks, And do not require the same color) for dyeing operations (dyed white or black). The minimum number of operations to ask. \ (1 \leq N, M, \leq 50.\)
Solution
To the target rectangle of the same color of the link block, to the adjacent color Unicom block Edge, the representative first to the block and all its adjacent blocks dyed into the same color, and then the block dyed into a different color. as to why is the best I do not know, the examination room to play out of the Qwq then the maximum depth of a tree in this figure is the answer that expands around the root for the dyeing center, and because the range is small, consider enumerating the roots.
Code
#include <bits/stdc++.h> #define SET (A, B) memset (A, B, sizeof (a)) #define for (I, J, K) for (register int i = j; I &L t;= K; ++i) #define Forr (I, J, K) for (register int i = j; I >= K; i) using namespace std;inline void File () {freopen ("Bzo J2638.in "," R ", stdin); Freopen ("Bzoj2638.out", "w", stdout);} const int N = 5, M = n * N; char s[n];const int dir[4][2] = {-1, 0, 0,-1, 1, 0, 0, 1};struct node {int sx, SY,} st[m];int a[n][n], N, M, Vis[n][n], Dep[m];int Id[m], cnt, ans, tt[m], vis1[n][n];vector<int> nxt[m];inline void dfs (int x, int y) {Vis[x][y] = cnt , id[cnt] = a[x][y]; ST[CNT] = (node) {x, y}; For (i, 0, 3) {int dx = x + dir[i][0], dy = y + dir[i][1]; if (DX < 1 | | dx > N | | dy < 1 | | dy > M | | a[x][y] ^ A[dx][dy] | | vis[dx][dy]) continue; DFS (dx, dy); }} inline void dfs1 (int x, int y) {vis1[x][y] = 1; For (i, 0, 3) {int dx = x + dir[i][0], dy = y + dir[i][1]; if (DX < 1 | | DX > n || Dy < 1 | | Dy > M | | Vis1[dx][dy]) continue; if (Vis[x][y] ^ Vis[dx][dy]) {if (!tt[vis[dx][dy]) Tt[vis[dx][dy]] = 1, nxt[vis[x][y]].push_back (Vis[dx][dy]) ; Continue } dfs1 (dx, dy); }} inline void Solve (int x) {Set (TT, 0); queue<int> Q; Q.push (x), tt[x] = 1, dep[x] = 1; while (! Q.empty ()) {int u = q.front (); Q.pop (); for (int i = 0, Sz = nxt[u].size (), I < SZ, + + i) {int v = nxt[u][i]; if (!tt[v]) Q.push (v), dep[v] = Dep[u] + 1, tt[v] = 1; }} int res = 0, p = 0; For (I, 1, CNT) if (Dep[i] > Res) res = dep[i], p = i; ans = min (ans, res-(id[p] = = 0));} int main () {File (); CIN >> n >> m; For (i, 1, n) {scanf ("%s", S + 1); For (J, 1, m) a[i][j] = s[j] = = ' B '; } for (I, 1, N) for (j, 1, m) if (!vis[i][j]) + + CNT, DFS (I, j); For (t, 1, CNT) set (VIS1, 0), set (TT, 0), DFS1 (ST[T].SX, st[t].sy); Ans = n *M + 10; For (I, 1, CNT) Solve (i); printf ("%d\n", ans); return 0;}
"Tsinsen A1039" "bzoj2638" black and white staining (bfs tree)