题目链接: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. typescript数据类型

    // 布尔类型 let isDone: boolean = false; // 数字类型 所有数字都是浮点数 number let decLiteral: number = 6; let hexLit ...

  2. 关于ASP.NET MVC的Html.BeginForm()方法

    http://zhidao.baidu.com/link?url=9j53URZJv2B9W-TPtQAaKCRbqIcYy2r3WNO0NDzciTON0EYj5Hhd3rl3UlIllK1CqOC ...

  3. python代码自动补全配置及Django入门Demo

    django入门代码示例小博客:https://pan.baidu.com/s/1pLjLPSv 1.自动补全功能 许多人都知道 iPython 有很好的自动补全能力,但是就未必知道 python 也 ...

  4. Linux /etc/issue 和 /etc/issue.net的作用和区别

    1./etc/motd /etc/motd即messageoftoday(布告栏信息),每次用户登录时,/etc/motd文件的内容会显示在用户的终端.系统管理员可以在文件中编辑系统活动消息,例如:管 ...

  5. 转:Xcode 删除文件后编译出现的missing file的警告

    进入“Missing File”对应的目录进行删除即可. 1.由于使用SVN导致的,可进行如下操作: # cd ~/iHost/Demo/sfsimonutility/SFSimonUtility/S ...

  6. Spring 中出现相同名称的 bean 的处理机制

    小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...

  7. Elasticsearch之Java实战

    资料 http://www.cnblogs.com/kamong/p/6099914.html 搭建Elasticsearch服务器

  8. SQL Server 2008 R2 企业版安装教程

    1 安装包解压 2 解压后,打开setup.exe文件,选择安装,显示如图: 3 选择全新安装或向现有安装添加功能 4 点确定 5 输入 企业版序列号:R88PF-GMCFT-KM2KR-4R7GB- ...

  9. 为什么JavaScript声明变量的时候鼓励加var关键字

    在JavaScript中,var用来声明变量,但是这个语法并不严格要求,很多时修改,我们可以直接使用一个变量而不用var声明它. var x = "XX"; y ="xx ...

  10. nc使用笔记

    netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它. 现内网中有两台机器:Mac: 192.168.1.109 Ka ...