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$即可 ...
随机推荐
- Docker入门之二镜像
Docker大部分的操作都是围绕三大核心概念:镜像.容器.仓库.学Docker首先得了解这几个词.这几个词可能平时也会有涉及,但Docker中可能不是同样得概念. 一.三大核心概念 镜像:可能在安装软 ...
- oc __weak和__strong的区别
1.先上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 id __weak obj=[[NSObject alloc]init]; NSLog(@"弱引 ...
- Javascript中的noscript
引言: 在浏览器日常火爆的时代,个大浏览器几乎都想占主导地位,争个你死我活,所以现在的各大浏览器都支持javascript脚本语言,但是在童鞋们,我们假设一下,万一哪个用户出于安全,把浏览器的java ...
- TETELaser Cutting System 不连续吹起的问题
TETELaser Cutting System 不连续吹起的问题 :配置 加工参数-->机器参数-->信号灯和激光器报警:黄灯索引==EX14 红灯索引==EX15 绿灯索引= ...
- 【BZOJ】 2463 [中山市选2009]谁能赢呢?(博弈论)
Description 小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的 ...
- bzoj1143 祭祀river(最大独立集)
[CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2175 Solved: 1098[Submit][Status] ...
- hdu3974 找上属的模拟
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- 神奇的版本库—————GIT
表示是第一次接触这个东东,然后疯狂百度了一波资料,然而=-=,完全不敢相信居然百度出了,GIT是全球最大同性交友网站...... 简直有点毁三观呐..好吧,其实按道理来说,这么解释也没有错欸,官方说明 ...
- keydown - > keypress - > keyup
英文输入法: 事件触发顺序:keydown - > keypress - > keyup 中文输入法: firfox:输入触发keydown,回车确认输入触发keyup chr ...
- For in 与For of 区别
For in 与For of 区别 for in遍历的是数组的索引(即键名)一般用于遍历对象:for(var index in obj):而for of遍历的是数组元素值:for(var value ...