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
题意: 给你一个棋盘 由不同颜色的棋子组成(两种)若干个联通快组成。 你每次可以翻转一个联通快使得最终的棋盘为同一种颜色。
思路: dfs 缩点建图,然后对每一个点遍历 寻找最优解。
代码:
#include<bits/stdc++.h>
#define N 45
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int v;
int step;
};
vector< int >ve[N*N];
int col[N][N];
char mp[N][N];
int n,m;
int cnt;
int ne[4][2]={-1,0, 0,-1, 1,0, 0,1 };
int vis[N*N];
void dfs(int x,int y,int co){
int tx,ty;
col[x][y]=co;
for(int i=0;i<4;i++){
tx=x+ne[i][0]; ty=y+ne[i][1];
if(tx<0||ty<0||ty>=m||tx>=n) continue;
if(mp[tx][ty]==mp[x][y]&&col[tx][ty]==-1){
dfs(tx,ty,co);
}
else if(mp[tx][ty]!=mp[x][y]&&col[tx][ty]!=-1){
int u=cnt; int v=col[tx][ty];
ve[u].push_back(v);
ve[v].push_back(u);
}
}
return ;
}
int bfs(int x){
node tmp;
node tmp1;
tmp.v=x; tmp.step=0;
memset(vis,0,sizeof(vis));
vis[x]=1;
queue<node >que;
que.push(tmp);
int ans=0;
int u,v,step;
while(!que.empty()){
tmp=que.front(); que.pop();
u=tmp.v; step=tmp.step;
ans=max(ans,step);
int sz=ve[u].size();
for(int i=0;i<sz;i++){
v=ve[u][i];
if(vis[v]) continue;
tmp1.v=v; tmp1.step=step+1;
que.push(tmp1);
vis[v]=1;
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
memset(col,-1,sizeof(col));
for(int i=1;i<=n*m;i++) ve[i].clear();
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
}
cnt=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(col[i][j]==-1){
col[i][j]=++cnt;
dfs(i,j,cnt);
}
}
}
/*
for(int i=0;i<n;i++){
for(int j=0;j<m;j++) printf("%d ",col[i][j]);
printf("\n");
}
*/
int ans=inf;
for(int i=1;i<=cnt;i++){
ans=min(ans,bfs(i));
}
printf("%d\n",ans);
}
return 0;
}
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