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.
Sample Input
2 2 2 OX OX 3 3 XOX OXO XOX
Sample Output
1 2
Hint
For the second sample, one optimal solution is:
Step 1. flip (2, 2)
XOX OOO XOX
Step 2. flip (1, 2)
XXX XXX XXX
给一个n*m的X O构成的格子(其实给的是n*n,但处理时都当做n*m,真是奇怪啊),对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色。
思路:
每次操作都将本身所在的连通块与和自己相邻的不同颜色的连通块变成同一种颜色,也就是变成一个连通块了,那么要使n次操作后全部变成一样的颜色,也就是从某点出发到达其余所有点。
dfs把连通块缩成点,然后相邻的连通块之间建边,枚举以每个点为根的情况,bfs求出每种情况的深度,取最小的即为答案。
#include<stdio.h> #include<string.h> #include<vector> #include<queue> #include <iostream> using namespace std; #define min(a,b) a>b?b:a struct node { int x; int step; }; char map[60][60]; int n, m; int cnt; int num[60][60]; int dx[4]={0,1,0,-1}; int dy[4]={-1,0,1,0}; vector<int>v[2000]; int judge(int x,int y) { if(x < 0 || x >= n || y < 0 ||y >= m) return 0; return 1; } void dfs( int x, int y, int dep, char c) { for( int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if(judge(nx, ny)) { if(map[nx][ny] == c) { if(num[nx][ny] == -1) { num[nx][ny] = dep; dfs(nx, ny, dep, c); } } else if(num[nx][ny] != -1) { int tmp = num[nx][ny]; v[dep].push_back(tmp); v[tmp].push_back(dep); } } } } int vis[2000]; int bfs(int id) { memset(vis,0,sizeof(vis)); int step = 0; queue<node>q; node tmp,next; tmp.step = 0; tmp.x = id; q.push(tmp); vis[id] = 1; while(!q.empty()) { node u = q.front(); q.pop(); if(step < u.step) step = u.step; int N = v[u.x].size(); for( int i = 0; i < N; i++) { int nu = v[u.x][i]; if(vis[nu] == 0) { next.step = u.step+1; next.x = nu; vis[nu] = 1; q.push(next); } } } return step; } int main() { int t, i, j; scanf("%d",&t); while(t--) { scanf("%d%d", &n, &m); for( i = 0; i < n; i++) scanf("%s",map[i]); memset(num, -1, sizeof(num)); for( i =0; i < n ;i++) v[i].clear(); cnt = 0; for( i = 0; i < n; i++) { for( j = 0; j < m; j++) if(num[i][j] == -1) { num[i][j] = cnt; dfs(i,j,cnt,map[i][j]); cnt++; } } // printf("%d ---- %d\n",n,m); // // for( i = 0; i < n; i++) // { // for( j = 0; j < m -1;j++) // printf("%d ",num[i][j]); // printf("%d\n",num[i][j]); // } int ans = 0x3f3f3f3f; for( i = 0; i < cnt; i++) { int tol = bfs(i); //printf("---%d\n",tol); ans = min(ans, tol); } printf("%d\n",ans); } }