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$即可 ...
随机推荐
- String StringBuffer StringBuilder 之间的区别
StringBuffer与StringBuilder的区别: StringBuffer是jdk1.0版本出来的,线程安全,效率低 StringBuilder是jdk1.5版本出来的,线程不安全,效率高 ...
- 用postal.js在AngularJS中实现订阅发布消息
点击查看AngularJS系列目录 用postal.js在AngularJS中实现event bus 用postal.js在AngularJS中实现event bus 理想状态下,在一个Angular ...
- 15 Validation
一.模型选择问题 如何选择? 视觉上 NO 不是所有资料都能可视化;人脑模型复杂度也得算上 通过Ein NO 容易过拟合;泛化能力差 通过Etest NO 能保证好的泛化,不过往往没法提前获得测试资料 ...
- vue实例讲解之axios的使用
本篇来讲解一下axios插件的使用,axios是用来做数据交互的插件. 这篇将基于vue实例讲解之vue-router的使用这个项目的源码进行拓展. axios的使用步骤: 1.安装axios npm ...
- Hive基础(3)---Fetch Task(转)
我们在执行hive代码的时候,一条简单的命令大部分都会转换成为mr代码在后台执行,但是有时候我们仅仅只是想获取一部分数据而已,仅仅是获取数据,还需要转化成为mr去执行吗?那个也太浪费时间和内存啦,所以 ...
- 分布式系统中生成全局ID的总结与思考
世间万物,都有自己唯一的标识,比如人,每个人都有自己的指纹(白夜追凶给我科普的,同卵双胞胎DNA一样,但指纹不一样).又如中国人,每个中国人有自己的身份证.对于计算机,很多时候,也需要为每一份数据生成 ...
- Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- 即时通信系统Openfire分析之五:会话管理
什么是会话? A拨了B的电话 电话接通 A问道:Are you OK? B回复:I have a bug! A挂了电话 这整个过程就是会话. 会话(Session)是一个客户与服务器之间的不中断的请求 ...
- Django 1.10中文文档-聚合
Django 数据库抽象API 描述了使用Django 查询来增删查改单个对象的方法. 然而,有时候你要获取的值需要根据一组对象聚合后才能得到. 这个主题指南描述了如何使用Django的查询来生成和返 ...
- Python自学笔记-time模块(转)
在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平台可能有所不同 ...