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. 保存数据到sdcard中去

    这里只贴上核心的代码:其它自行脑补!

  2. Linux Expect自动化交互脚本简介

    相关资料 维基百科:Expect SourceForge:The Expect Home Page TCL脚本言语简介 由于Expect是建立在TCL语言基础上的一个工具,因此首先检查一些TCL常见语 ...

  3. Java中的HTTP通信技术详解

     1.使用HTTP的Get方式读取网络数据       class ReadByGet extends Thread{ @Override public void run(){ URL url = n ...

  4. AngularJS系列-翻译官网

    公司之前一直用的Web前台框架是Knockout,我们通常直接叫ko,有看过汤姆大叔的KO系列,也有在用,发现有时候用得不太顺手.本人是会WPF的,所以MVVM也是比较熟悉的,学ko也是很快就把汤姆大 ...

  5. javascript篇-----数据类型

    ECMAScript中一共有6种数据类型,其中包括5种基本数据类型(Undefined,Null,Boolean,Number,String)以及一种复杂数据类型(Object).[ES6增加多了一种 ...

  6. MySQL主从同步和读写分离的配置

    主服务器:192.168.1.126 从服务器:192.168.1.163 amoeba代理服务器:192.168.1.237 系统全部是CentOS 6.7 1.配置主从同步 1.1.修改主服务器( ...

  7. 进入css3动画世界(二)

    进入css3动画世界(二) 今天主要来讲transition和transform入门,以后会用这两种属性配合做一些动效. 注:本文面向前端css3动画入门人员,我对这个也了解不深,如本文写的有纰漏请指 ...

  8. JQ重复注册问题

    开发中常常会碰到事件重复注册,简单总结一下解决方法. (1)bind注册事件 $('...').unbind().bind('...',function(){}) (2)live注册事件 $('... ...

  9. Ionic3学习笔记(四)修改返回按钮文字、颜色

    本文为原创文章,转载请标明出处 目录 修改返回按钮文字 修改返回按钮颜色 1. 修改返回按钮文字 参考官网 Ionic API---Config 文档 可在 ./src/app/app.module. ...

  10. Python单元测试框架

    目录 概况 系统要求 使用PyUnit构建自己的测试 安装 测试用例介绍 创建一个简单测试用例 复用设置代码:创建固件 包含多个测试方法的测试用例类 将测试用例聚合成测试套件 嵌套测试用例 测试代码的 ...