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.
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.
For each test case, output the minimum steps needed to make all cells in the same color.
2 2 2 OX OX 3 3 XOX OXO XOX
1 2
For the second sample, one optimal solution is:
Step 1. flip (2, 2)
XOX OOO XOX
Step 2. flip (1, 2)
XXX XXX XXX
相当于每次从一个联通块只能到达相邻的不同颜色的联通块,由于棋盘是二维的,对于每个联通块跑一遍bfs就可以找到离他最远的联通块要几次操作才能得到,为了加速bfs要缩点。水题..
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <set> using namespace std; typedef long long LL; const int N = 40 + 10; char g[N][N]; int n, m; int vis[N][N]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; set<int>Map[1600 + 10]; struct Edge{int to, nxt;}e[(1610 * 4) << 1]; int head[1610]; int tot; void initGraph(){ tot = 0; memset(head, 0, sizeof head); } void add(int u, int v){ e[++tot].to = v; e[tot].nxt = head[u]; head[u] = tot; e[++tot].to = u; e[tot].nxt = head[v]; head[v] = tot; } int sz; void dfs(int i, int j, char t){ int x, y; for(int k = 0; k < 4; k++){ x = i + dx[k]; y = j + dy[k]; int u = sz, v = vis[x][y]; if (v && g[x][y] != t && !Map[u].count(v)){ Map[u].insert(v); Map[v].insert(u); add(u, v); }else if (!v && g[x][y] == t){ vis[x][y] = sz; dfs(x, y, t); } } } int d[1600 + 10]; int bfs(int k){ int re = 0; queue<int>q; int x, y; memset(d, 0, sizeof d); q.push(k); d[k] = 1; while(!q.empty()){ x = q.front(); q.pop(); for(int i = head[x]; i; i = e[i].nxt){ y = e[i].to; if (d[y]) continue; q.push(y); d[y] = d[x]+1; re = max(re, d[y] - 1); } } return re; } int main() { // freopen("data.in", "r", stdin); // freopen("data.out", "w", stdout); int T; scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++){ scanf("%s", g[i]+1); } for(int i = 1; i <= n; i++){ g[i][0] = g[i][m+1] = '#'; } for(int i = 1; i <= m; i++){ g[0][i] = g[n+1][i] = '#'; } sz = 0; memset(vis, 0, sizeof vis); for(int i = 0; i < 1610; i++) Map[i].clear(); initGraph(); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if (vis[i][j]) continue; vis[i][j] = ++sz; dfs(i, j, g[i][j]); } } int ans = sz; for(int i = 1; i <= sz; i++){ ans = min(ans, bfs(i)); } printf("%d\n", ans); } return 0; }