# ZOJ 3781 Paint the Grid Reloaded（BFS）

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.

``` 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
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) {
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])
72                 }
73             }
74
75         for(int i = 1; i <= id_cnt; ++i) {
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
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。