ZOJ 3781 Paint the Grid Reloaded(BFS)

时间:2023-03-08 17:00:03

题目链接: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 <= NM <= 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):

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std; const int MAXN = ;
const int MAXV = ; vector<int> adj[MAXV];
char mat[MAXN][MAXN];
int a[MAXN][MAXN];
int n, m, T, id_cnt; int fx[] = {-, , , };
int fy[] = {, , , -}; void dfs(int r, int c, int col) {
if(a[r][c]) return ;
a[r][c] = col;
for(int f = ; f < ; ++f) {
int new_r = r + fx[f], new_c = c + fy[f];
if(mat[new_r][new_c] == mat[r][c])
dfs(new_r, new_c, col);
}
} void add_edge(int u, int v) {
adj[u].push_back(v);
} int dis[MAXV];
int bfs(int u) {
memset(dis + , 0x3f, id_cnt * sizeof(dis[]));
dis[u] = ;
queue<int> que; que.push(u);
while(!que.empty()) {
int u = que.front(); que.pop();
for(auto v : adj[u]) {
if(dis[v] > dis[u] + ) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
return *max_element(dis + , dis + id_cnt + );
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
memset(mat, , sizeof(mat));
memset(a, , sizeof(a));
for(int i = ; i <= n; ++i) scanf("%s", mat[i] + ); id_cnt = ;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) if(!a[i][j])
dfs(i, j, ++id_cnt); for(int i = ; i <= id_cnt; ++i) adj[i].clear();
for(int r = ; r <= n; ++r)
for(int c = ; c <= m; ++c) {
for(int f = ; f < ; ++f) {
int new_r = r + fx[f], new_c = c + fy[f];
if(mat[new_r][new_c] != && mat[new_r][new_c] != mat[r][c])
add_edge(a[r][c], a[new_r][new_c]);
}
} for(int i = ; i <= id_cnt; ++i) {
sort(adj[i].begin(), adj[i].end());
adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end());
}
int res = ;
for(int i = ; i <= id_cnt; ++i)
res = min(res, bfs(i));
printf("%d\n", res);
}
}