Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781
Leo has a grid with N rows and M columns. All cells is painted with either black or white initially.
Both cells A B and is called connected if they share an edge and they is in the same color, or there exi STS a cell C connected to both A and B .
Leo wants to paint the grid with the same color. He can make it do in multiple steps. At each step Leo can choose a cell and the flip the color (from black to white or from white to black) of all cells connected to it. Leo wants to know the minimum number of steps he needs to make all cells in the same color.
Input
There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:
The first line contains integers N M and (1 <= N , M <= 40). Then N lines follow. Each line contains a string with N characters. Each character are either ' X ' (black) or ' O ' (white) indicates the initial color of the cells.
Output
For each test case, the output of the minimum steps needed to make all cells in the same color.
The main topic: to a n*m matrix, each position is O or x, each click can make a connected o/x all become x/o, ask all into O or x how many times.
Idea: First, if all connected blocks are treated as a point, the adjacent connected blocks will have one edge, and a plan can be obtained. All edges are treated as virtual edges, and then each click can turn a point and the imaginary edge of all connections to a point with its solid edge into a solid edge.
Conclusion: There is an optimal solution, and each click is the same point. Then only the maximum value of the shortest path to other points of each point x is required p[x] (that is, the farthest point), the answer is Min{p[x]}.
Proof: Suppose there is an optimal solution for ans, which consists of k points. If k=1, the proof is over. Otherwise, k>1:
Now defines the set S (x, V) = {y, dis (x, y) ≤v}, called the V-connected component of X. where DIS (x, y) is the shortest path length of x, Y.
Suppose that the X in the optimal solution has a click of cnt[x].
Select any two adjacent cnt-connected components in K points, set to a, B.
Assuming the optimal solution of a click on Cnt[a], B clicked on Cnt[b], then there is D=dis (A, b) ≤cnt[a]+cnt[b].
Suppose the shortest path from A to B is p[0]=a, p[1], p[2]......p[d]=b.
Then if we only click on P[cnt[b]], click on Cnt[a]+cnt[b], then first click Cnt[b] under the inevitable can reach point a, after Cnt[a] can overwrite the original s (A, cnt[a]).
And because D-cnt[b]≤cnt[a], then click Cnt[a] under the inevitable can reach point B, after the cnt[b] can cover the original s (b, cnt[b]).
Then A, B can be replaced with p[cnt[b]], the number of steps of the solution is unchanged, is still the optimal solution.
Repeat the process until the remaining point is completed.
Code (100MS):
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <iostream>5#include <numeric>6#include <queue>7#include <vector>8 using namespacestd;9 Ten Const intMAXN = -; One Const intMAXV =1610; A -vector<int>ADJ[MAXV]; - CharMAT[MAXN][MAXN]; the intA[MAXN][MAXN]; - intN, M, T, id_cnt; - - intFx[] = {-1,0,1,0}; + intFy[] = {0,1,0, -1}; - + voidDfsintRintCintCol) { A if(A[r][c])return ; atA[R][C] =Col; - for(intf =0; F <4; ++f) { - intNew_r = r + fx[f], New_c = c +Fy[f]; - if(Mat[new_r][new_c] = =Mat[r][c]) - dfs (New_r, New_c, col); - } in } - to voidAdd_edge (intUintv) { + Adj[u].push_back (v); - } the * intDIS[MAXV]; $ intBFsintu) {Panax Notoginsengmemset (dis +1,0x3f, id_cnt *sizeof(dis[0])); -Dis[u] =0; thequeue<int>que; Que.push (u); + while(!Que.empty ()) { A intU =Que.front (); Que.pop (); the for(Auto V:adj[u]) { + if(Dis[v] > Dis[u] +1) { -DIS[V] = Dis[u] +1; $ Que.push (v); $ } - } - } the return*max_element (dis +1, dis + id_cnt +1); - }Wuyi the intMain () { -scanf"%d", &T); Wu while(t--) { -scanf"%d%d", &n, &m); AboutMemset (Mat,0,sizeof(MAT)); $Memset (A,0,sizeof(a)); - for(inti =1; I <= N; ++i) scanf ("%s", Mat[i] +1); - -id_cnt =0; A for(inti =1; I <= N; ++i) + for(intj =1; J <= M; ++J)if(!A[i][j]) theDFS (I, J, + +)id_cnt); - $ for(inti =1; I <= id_cnt; ++i) adj[i].clear (); the for(intR =1; R <= N; ++R) the for(intc =1; C <= m; ++c) { the for(intf =0; F <4; ++f) { the intNew_r = r + fx[f], New_c = c +Fy[f]; - if(Mat[new_r][new_c]! =0&& Mat[new_r][new_c]! =Mat[r][c]) in Add_edge (A[r][c], A[new_r][new_c]); the } the } About the for(inti =1; I <= id_cnt; ++i) { the sort (Adj[i].begin (), Adj[i].end ()); the adj[i].erase (Unique (Adj[i].begin (), Adj[i].end ()), Adj[i].end ()); + } - intres =1000000; the for(inti =1; I <= id_cnt; ++i)Bayires =min (res, BFS (i)); theprintf"%d\n", res); the } -}View Code
ZOJ 3781 Paint the Grid Reloaded (BFS)