Paint the Grid Reloaded
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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把联通块编号,然后bfs找出以每一个联通块为顶点时的深度。所有深度的最小值就是ans.
做了这道题,重新温习了bfs模板。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAX=0xfffffff;
char mp[45][45];
int num[1610],cnt,n,m,vis[1610];
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
vector<int> v[1600];
struct node
{
int d,id;
};
void dfs(int x,int y)
{
num[x*m+y]=cnt;
for(int i=0;i<4;i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m)
{
if(mp[xx][yy]==mp[x][y])
{if(num[xx*m+yy]==-1) dfs(xx,yy);}
else if(num[xx*m+yy]!=-1)
{
int temp=num[xx*m+yy];
v[cnt].push_back(temp);
v[temp].push_back(cnt);
}
}
}
}
int bfs(int no)
{
queue<node>Q;
node q;
q.d=0,q.id=no;
Q.push(q);
int ret=0;
memset(vis,0,sizeof(vis));
vis[q.id]=1;
while(!Q.empty())
{
node p=Q.front();Q.pop();
if(ret<p.d) ret=p.d;
q.d=p.d+1;
for(int i=0;i<v[p.id].size();i++)
{
q.id=v[p.id][i];
if(!vis[q.id])
{
vis[q.id]=1;
Q.push(q);
}
}
}
return ret;
}
/*int bfs(int no)
{
queue<node> q;
node tmp;
tmp.d=0;
tmp.id=no;
q.push(tmp);
int ret=0;
memset(vis,0,sizeof vis);
vis[tmp.id]=1;
while(!q.empty())
{
node now=q.front();
q.pop();
if(ret<now.d) ret=now.d;
tmp.d=now.d+1;
for(int j=0;j<v[now.id].size();j++)
{
tmp.id=v[now.id][j];
if(!vis[tmp.id])
{
vis[tmp.id]=1;
q.push(tmp);
}
}
}
return ret;
}*/
int main( )
{
freopen("1.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++) scanf("%s",mp[i]);
for(int i=0;i<n*m;i++) v[i].clear();
memset(num,-1,sizeof(num));
cnt=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(num[i*m+j]==-1)
{
num[i*m+j]=cnt;
dfs(i,j);
cnt++;
}
int ans=MAX;
for(int i=0;i<cnt;i++)
ans=min(ans,bfs(i));
printf("%d\n",ans);
}
return 0;
}