Paint the Grid Reloaded ZOJ - 3781 图论变形

时间:2021-06-06 10:52:09
Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

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.

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
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
using namespace std;
vector<int> c[];
char a[][];
int b[][],n,m,bcnt,vi[];
int w[][]= {{,},{-,},{,},{,-}};
void dfs(int r,int c1,int x)
{
b[r][c1]=x;
int i,rr,cc;
for(i=; i<; i++)
{
rr=r+w[i][];
cc=c1+w[i][];
if(rr<n&&rr>=&&cc<m&&cc>=&&!b[rr][cc]&&a[rr][cc]==a[r][c1])
{
dfs(rr,cc,x);
}
}
}
void build()
{
int i,j,k,rr,cc,x,y;
for(i=; i<=bcnt; i++)c[i].clear();
map<pair<int,int>,int>e;
e.clear();
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
for(k=; k<; k++)
{
rr=i+w[k][];
cc=j+w[k][];
//
if(rr<n&&rr>=&&cc<m&&cc>=&&b[rr][cc]!=b[i][j])
{
x=b[rr][cc],y=b[i][j];
if(x>y)swap(x,y);
e.insert(make_pair(make_pair(x,y),));
}
}
}
}
for(map<pair<int,int>,int>::iterator it=e.begin();it!=e.end();it++)
{
x=(*it).first.first,y=(*it).first.second;
c[x].push_back(y);
c[y].push_back(x);
//cout<<(*it).first.first<<" "<<(*it).first.second<<endl;
}
}
void DFS()
{
memset(b,,sizeof(b));
int i,j;
bcnt=;
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
if(!b[i][j])
dfs(i,j,bcnt++);
}
}
build();
/* for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}*/
}
int solve(int x)
{
memset(vi,,sizeof(vi));
queue<pair<int,int> >q;
while(!q.empty())q.pop();
pair<int,int>now;
q.push(make_pair(x,));
vi[x]=;
int i;
now.second=;
while(!q.empty())
{
now=q.front();
q.pop();
for(i=;i<c[now.first].size();i++)
{
if(!vi[c[now.first][i]])
q.push(make_pair(c[now.first][i],now.second+)),vi[c[now.first][i]]=;
}
}
return now.second;
}
int main()
{
int t,i,mina;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=; i<n; i++)scanf("%s",a[i]);
DFS();
mina=;
for(i=;i<bcnt;i++)
mina=min(mina,solve(i));
printf("%d\n",mina);
}
}