题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268

题目大意:字符一样并且相邻的即为连通。每次可翻转一个连通块X(O)的颜色,问至少改变几次使得图上所有字符都相等。

解题思路:

1) dfs( 建图 ) ,因为翻转的时候每翻转连通块中一个整个连通块都翻转,
这样你可以将其看成一个有边相连的无向图,每个边的两个顶点颜色都不一样。

2) bfs( 寻找最优解 ) , 建完图后就需要翻转计算最优解,可以枚举从每一点开始翻转所得到的最小步数,那怎样寻找最小步数 ?
假如从 v 这个顶点出发,那么与 v 相邻的顶点颜色必定都与 v 相反,so~> 你只需要把 v的颜色翻转,
v与它相邻的所有顶点都是同一个颜色,这时可以把这些顶点看成一个连通块(顶点),再向四周扩展,
再次扩展时,周围的颜色必定与此连通块颜色相反,再将它变成与周围顶点相同的颜色,
这样又合并成为一个连通块……这样一直进行下去取最大步数。最后从最大步数中取出最小的步数即为最优解。

代码:

 #include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=+;
const int INF=0x3f3f3f3f;
const double eps=1e-; int n,m,cnt;
int path[N*N],vis[N][N];
int d[][]={,,,,,-,-,};
char mp[N][N];
vector<int>v[N*N]; bool judge(int x,int y){
if(x>=&&y>=&&x<=n&&y<=m&&!vis[x][y])
return true;
return false;
} void dfs(int x,int y){
vis[x][y]=cnt;
for(int i=;i<;i++){
int xx=x+d[i][];
int yy=y+d[i][];
if(judge(xx,yy)&&mp[x][y]==mp[xx][yy]){
dfs(xx,yy);
}
}
} int bfs(int st){
memset(path,-,sizeof(path));
queue<int>q;
q.push(st);
path[st]=;
while(!q.empty()){
int k=q.front();
q.pop();
for(int i=;i<v[k].size();i++){
int t=v[k][i];
if(path[t]==-){
path[t]=path[k]+;
q.push(t);
}
}
}
} int main(){
FAST_IO;
int t;
cin>>t;
while(t--){
memset(vis,,sizeof(vis));
cin>>n>>m;
for(int i=;i<=n*m;i++) v[i].clear();
for(int i=;i<=n;i++){
cin>>mp[i]+;
}
cnt=;
//找连通块,缩点
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(!vis[i][j]){
++cnt;
dfs(i,j);
}
}
}
//连通块间建边
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
for(int k=;k<;k++){
int xx=i+d[k][];
int yy=j+d[k][];
if(vis[xx][yy]!=vis[i][j]){
int t1=vis[i][j],t2=vis[xx][yy];
v[t1].push_back(t2);
}
}
}
}
//枚举每个连通块为起点,每次找出最大路径,找出这些最大路径中的最小值
int ans=INF;
for(int i=;i<=cnt;i++){
bfs(i);
int mmax=-INF;
for(int j=;j<=cnt;j++){
mmax=max(mmax,path[j]);
}
ans=min(ans,mmax);
}
cout<<ans<<endl;
}
return ;
}

ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. ZOJ 3781 Paint the Grid Reloaded

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

  4. ZOJ - 3781 Paint the Grid Reloaded 题解

    题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...

  5. ZOJ 3781 Paint the Grid Reloaded 连通块

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...

  6. 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 ...

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

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

  8. 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构成的格子,对 ...

  9. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

随机推荐

  1. Java List /ArrayList 三种遍历方法

    java list三种遍历方法性能比较http://www.cnblogs.com/riskyer/p/3320357.html JAVA LIST 遍历http://blog.csdn.net/lo ...

  2. 丁酉年六月十一ACM模拟赛

    似乎该写题解了.今天模拟ACM,10道题(本来还有2道被删了),9道都来自BZOJ,中间我做过2道.那么说,今天Solv.便大大增多了(但还是不如强大的Amphetamine). 题单及一句话题解如下 ...

  3. Web前端之Javascript详解20180330

    一.javascript概述 javascript是基于对象和事件的脚本语言. 特点: 1.安全性(不允许直接访问本地硬盘(因为是被远程的浏览器解释)),它可以做的就是信息的动态交互 2.跨平台性(只 ...

  4. Tensorflow实战Google深度学习框架-总结-1

    第一章:深度学习简介   1⃣️应用有 1.计算机视觉 2.语音识别 3.自然语言处理 4.人机博弈   2⃣️深度学习,机器学习,AI 的关系

  5. Tomcat性能调优及JVM内存工作原理

    Java性能优化方向:代码运算性能.内存回收.应用配置. 注:影响Java程序主要原因是垃圾回收,下面会重点介绍这方面 代码层优化:避免过多循环嵌套.调用和复杂逻辑.Tomcat调优主要内容如下:1. ...

  6. django中模板变量与内置标签以及过滤器

    本文参考 官方文档 . 一  模板变量 格式: {{ variable_name }} variable_name   命名规则与变量命名规则类似,允许字符数字下划线,不允许标点. variable_ ...

  7. 又一家药企IPO被拒,原因竟然是……

    版权所有,未经授权不得转载,QQ:231469242 导读 近日,中国证监会官网发布公告,河南润弘制药首发未IPO能通过,成为今年第4家IPO被否的制药企业.中国证监会拒绝润弘制药的原因是润弘制药产品 ...

  8. RCNN,fast R-CNN,faster R-CNN

    转自:https://www.cnblogs.com/skyfsm/p/6806246.html object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别. ...

  9. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

  10. awk例子

     ls |awk -F . '{print $1}'|awk -F '-[0-9]' '{print $1}'