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
/*
题意:给你一个只有元素O/X的矩阵,有这样一种操作每次你点一个元素的时候,这个元素所在的联通块(颜色相同)就会翻转,
问你最少经过几次操作可以将所有的元素变成颜色统一
初步思路:很隐含的最长路,先找出所有的联通块,将相邻的联通块进行建边,枚举从每一个点开始的最长路,就是从这个点
开始翻需要的最小操作,因为相邻的联通块只需要一次操作就可以变成相同的颜色。
*/
#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define pb push_back
using namespace std;
struct node{
int x,step;
node(){}
node(int a,int b):x(a),step(b){}
};
int t;
int n,m;
char mapn[][];
int num[][];//表示所在的联通块
int cnt;//表示联通块的数量
int dir[][]={,,-,,,,,-};
int minstep;
int judge[][];//判断是不是建了重边
vector<int> edge[];
bool ok(int x,int y){
if(x<||x>=n||y<||y>=m)
return false;
return true;
}
void dfs(int x,int y,int cur,char op){
for(int i=;i<;i++){
int fx=x+dir[i][];
int fy=y+dir[i][];
if(ok(fx,fy)==false)
continue;
if(mapn[fx][fy]==op){//是一个联通块的
if(num[fx][fy]==-){//如果他还没有被标记
num[fx][fy]=cur;
dfs(fx,fy,cur,op);
}
}else{//如果不是一个联通块的那么就要建边了
if(num[fx][fy]!=-){//建双向边
if(!judge[cur][num[fx][fy]]){
edge[cur].pb(num[fx][fy]);
edge[num[fx][fy]].pb(cur);
judge[cur][num[fx][fy]]=;
judge[num[fx][fy]][cur]=;
}
}
}
}
}
int bfs(int x){
int vis[];
memset(vis,,sizeof vis);
node start(x,),Next;
vis[x]=;
queue<node>q;
int times=;
q.push(start);
while(!q.empty()){
Next=q.front();
q.pop();
// cout<<Next.x<<endl;
if(Next.step>times){
times=Next.step;
}
for(int i=;i<edge[Next.x].size();i++){
int v=edge[Next.x][i];
if(v==Next.x) continue;//防止死循环
if(!vis[v]){
q.push(node(v,Next.step+));
vis[v]=;
}
}
}
return times;
}
void init(){
cnt=;
memset(num,-,sizeof num);
memset(judge,,sizeof judge);
for(int i=;i<;i++){
edge[i].clear();
}
minstep=;
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",mapn[i]);
}//输入
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(num[i][j]==-){
num[i][j]=++cnt;
dfs(i,j,cnt,mapn[i][j]);
}
}
}//寻找联通块进行建边
for(int i=;i<=cnt;i++){
// cout<<"i="<<i<<endl;
int tmp=bfs(i);
// cout<<tmp<<endl;
minstep=min(minstep,tmp);
}
printf("%d\n",minstep);
}
return ;
}

Paint the Grid Reloaded(缩点,DFS+BFS)的更多相关文章

  1. ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)

    Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows ...

  2. Paint the Grid Reloaded ZOJ - 3781 图论变形

    Paint the Grid Reloaded Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %ll ...

  3. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  4. 【最短路+bfs+缩点】Paint the Grid Reloaded ZOJ - 3781

    题目: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...

  5. ZOJ 3781 Paint the Grid Reloaded(BFS)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...

  6. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  7. 2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS

    原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 给一个n*m的X,O构成的格子,对 ...

  8. [ZOJ3781]Paint the Grid Reloaded

    思路: 先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$. 注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上. 一开始和网上的题解对拍$T=1$ ...

  9. ZOJ 3781 Paint the Grid Reloaded

    枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...

随机推荐

  1. 记一次 node.js 的populate用法

    最近在学习node.js做一个简单的博客的网站,用的express框架和mongodb的数据库.之前没有接触过这个数据库,所有在一开始写的时候遇到了一些问题,如何初始化model类型,又如何实现简单的 ...

  2. ssl协议以及生成

    一.https协议https是一安全为目标的httpt通道,简单讲师http的安全版.即http下加入ssl层,https的安全基础是ssl,因此加密的详细内容就需要ssl.http和https的区别 ...

  3. Java中的类型转换(Integer、Long、String)

    这段时间将项目中一个模块参照C++源代码,实现一个JAVA版.主要功能是将一些字段信息转换为String类型,传输后可以进行解析. Integer.Long转为String,Java本身提供了这种转换 ...

  4. [js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解

    路径在canvas绘图中,经常被用到,是一个非常重要的概念. 比如:我们要在canvas画出3条直线,要求用不同的颜色加以区分. <style> body { background: #0 ...

  5. MySQL之最基本命令

    前言:以下是数据库最基础最常用的命令,特别适用初学者练习,希望通过不断练习这些命令来熟练操作.巩固基础,因为只有不断地练习才能将知识真正变成自己的东西. 快速查看以下内容: 操作 命令 创建数据库 C ...

  6. PHP浮点型(float)转换为整形(int)/round()保留小数点后几位

    round(x,y); x:需要转换的变量 y:保留几位小数 <?php echo round(3.112312321) //输出3 echo round(3.112312321,3) //输出 ...

  7. M-定在下边的区域

    1 效果 2 布局 3 样式

  8. HDU4278 Faulty Odometerd

    开始以为是容斥原理,想着做一下,应该是可以用容斥解决的,有空再过来写一下.题解是进制转换,开始没想到,不过很好理解. 如在10进制里: 1254=  (1*10^3 + 2*10^2 + 5* 10^ ...

  9. Linux向文件添加内容的几种方法

    例如,要想test.txt文件添加内容"I am a boy",test.txt在当前目录中 方法一:vi编辑法 打开终端,输入vi test.txt 回车,按a或i进入编辑模式, ...

  10. win10 UWP 剪贴板 Clipboard

    win10 UWP 剪贴板 Clipboard使用Windows.ApplicationModel.DataTransfer.Clipboard 设置文本 DataPackage dataPackag ...