ZOJ 3781 Paint the Grid Reloaded(BFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781

Leo has a grid with N rows and M columns. All cells are painted with either black or white initially.

Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both A and B.

Leo wants to paint the grid with the same color. He can make it done in multiple steps. At each step Leo can choose a cell and 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 are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N and M (1 <= N, M <= 40). Then N lines follow. Each line contains a string with N characters. Each character is either ‘X’ (black) or ‘O’ (white) indicates the initial color of the cells.

Output

For each test case, output the minimum steps needed to make all cells in the same color.

 

题目大意:给一个N*M的矩阵,每个位置是O或X,每次点击可以使一片连通的O/X全部变成X/O,问全部变成O或X要点多少次。

思路:首先如果把所有连通块看成一个点,相邻的连通块连一条边,可以得到一个平面图。先把所有边当成虚边,然后每次点击,可以把一个点和与其有实边连通的点的所有连接的虚边变成实边。

结论:存在最优解,每次点击的是同一个点。那么只需求出每个点x的到其他点的最短路径的最大值p[x](即最远点),答案为min{p[x]}。

证明:假设存在一个最优解ans,包含k个点。若k=1,证明结束。否则,k>1:

现定义集合S(x, v) = {y, dis(x, y) ≤ v},称为x的v-连通分量。其中dis(x,y)为x、y的最短路径长度。

假设最优解中x的点击次数为cnt[x]。

先选择k个点中任意两个相邻的cnt-连通分量,设为a、b。

假设最优解中a点击了cnt[a]下,b点击了cnt[b]下,那么有d=dis(a,b)≤cnt[a]+cnt[b]。

假设a到b的最短路径为p[0]=a、p[1]、p[2]……p[d]=b。

那么若我们只点击p[cnt[b]],点击cnt[a]+cnt[b]下,那么首先点击cnt[b]下必然可以到达点a,之后cnt[a]下可以覆盖原来的S(a, cnt[a])。

又因为d-cnt[b]≤cnt[a],那么点击cnt[a]下必然可以到达点b,之后的cnt[b]下可以覆盖原来的S(b, cnt[b])。

那么可以把a、b替换成p[cnt[b]],解的步数不变,依然是最优解。

不断重复上述过程直到剩下一个点,证毕。

 

代码(100ms):

《ZOJ 3781 Paint the Grid Reloaded(BFS)》
《ZOJ 3781 Paint the Grid Reloaded(BFS)》

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <numeric>
 6 #include <queue>
 7 #include <vector>
 8 using namespace std;
 9 
10 const int MAXN = 44;
11 const int MAXV = 1610;
12 
13 vector<int> adj[MAXV];
14 char mat[MAXN][MAXN];
15 int a[MAXN][MAXN];
16 int n, m, T, id_cnt;
17 
18 int fx[] = {-1, 0, 1, 0};
19 int fy[] = {0, 1, 0, -1};
20 
21 void dfs(int r, int c, int col) {
22     if(a[r][c]) return ;
23     a[r][c] = col;
24     for(int f = 0; f < 4; ++f) {
25         int new_r = r + fx[f], new_c = c + fy[f];
26         if(mat[new_r][new_c] == mat[r][c])
27             dfs(new_r, new_c, col);
28     }
29 }
30 
31 void add_edge(int u, int v) {
32     adj[u].push_back(v);
33 }
34 
35 int dis[MAXV];
36 int bfs(int u) {
37     memset(dis + 1, 0x3f, id_cnt * sizeof(dis[0]));
38     dis[u] = 0;
39     queue<int> que; que.push(u);
40     while(!que.empty()) {
41         int u = que.front(); que.pop();
42         for(auto v : adj[u]) {
43             if(dis[v] > dis[u] + 1) {
44                 dis[v] = dis[u] + 1;
45                 que.push(v);
46             }
47         }
48     }
49     return *max_element(dis + 1, dis + id_cnt + 1);
50 }
51 
52 int main() {
53     scanf("%d", &T);
54     while(T--) {
55         scanf("%d%d", &n, &m);
56         memset(mat, 0, sizeof(mat));
57         memset(a, 0, sizeof(a));
58         for(int i = 1; i <= n; ++i) scanf("%s", mat[i] + 1);
59 
60         id_cnt = 0;
61         for(int i = 1; i <= n; ++i)
62             for(int j = 1; j <= m; ++j) if(!a[i][j])
63                 dfs(i, j, ++id_cnt);
64 
65         for(int i = 1; i <= id_cnt; ++i) adj[i].clear();
66         for(int r = 1; r <= n; ++r)
67             for(int c = 1; c <= m; ++c) {
68                 for(int f = 0; f < 4; ++f) {
69                     int new_r = r + fx[f], new_c = c + fy[f];
70                     if(mat[new_r][new_c] != 0 && mat[new_r][new_c] != mat[r][c])
71                         add_edge(a[r][c], a[new_r][new_c]);
72                 }
73             }
74 
75         for(int i = 1; i <= id_cnt; ++i) {
76             sort(adj[i].begin(), adj[i].end());
77             adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end());
78         }
79         int res = 1000000;
80         for(int i = 1; i <= id_cnt; ++i)
81             res = min(res, bfs(i));
82         printf("%d\n", res);
83     }
84 }

View Code

 

    原文作者:Oyking
    原文地址: https://www.cnblogs.com/oyking/p/4430723.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞