Paint the Grid Reloaded(缩点,DFS+BFS)
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
/*
题意:给你一个只有元素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)的更多相关文章
- 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 ...
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- 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 ...
- 【最短路+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 ...
- 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 ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- 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构成的格子,对 ...
- [ZOJ3781]Paint the Grid Reloaded
思路: 先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$. 注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上. 一开始和网上的题解对拍$T=1$ ...
- ZOJ 3781 Paint the Grid Reloaded
枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...
随机推荐
- 每天学点SpringMVC-拦截器
1. 先写个Hello World 1.1 写一个Interceptor class并实现HandlerInterceptor接口 public class FirstInterceptor impl ...
- 进入css3动画世界(一)
其实我做css3动画也没有多久,这篇文章目标人群是css3动画的新手,不喜勿喷. 分类 目前我接触到的css3动画有2类:一种是transition的,另一种是@keyframes的. 两者的区别就是 ...
- mybatis错误——java.io.IOException: Could not find resource com/xxx/xxxMapper.xml
在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang. ...
- web版的tty
1.wetty Wetty是使用Node.js和websockets开发的一个开源`Web-based SSH` 2.环境配置 2.1.配置epel源 [epel] name=epel baseu ...
- codevs 种树3
codevs上的题目,自从wikioi改名后,就不怎么做题了. 这道题的话注释在代码中就可以了,还是求最长路,相较返回如果中间可以种多个的话,那就种越多越好,因为这样可以减少种的棵树, 所以这个i与i ...
- JS方法总结
1.普通的方法定义 function(){ } 2.变量方法定义 var text=function(){ } 3.对象方法定义 text:function(){ } 4.ES6 text(x=0,y ...
- springMVC中的redirect和forward区别?
1.forward在跳转后可以取到message值,redirect在跳转后无法取到message值. 2.forward跳转后地址栏URL不会改变,而redirect会改变.
- Python实战之set学习笔记及简单练习
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__' ...
- python的urlparse
urlparse主要是URL的分解和拼接,分析出URL中的各项参数,可以被其他的URL使用. 主要的函数有: 1.urlparse 将URL分解为6个片段,返回一个元组,包括协议.基地址.相对地址等等 ...
- Color Blender---在线渐变色带生成器
Color Blender是一个很有用的在线渐变色带生成器,它可以在两种颜色之间,自动生成过渡色,对网页设计师来说是一个不错的颜色调配工具. Color Blender的使用方法很简单,你只 ...