题目链接: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. Android Launcher分析和修改

    Android Launcher分析和修改 http://www.cnblogs.com/mythou/category/499819.html Android Launcher分析和修改1——Lau ...

  2. JAVA中properties基本用法

    转载 源地址不详 java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式, ...

  3. 使用 xhprof 进行 php 的性能分析

    基于本机环境(php7,macos) 1.xhprof 扩展 php7 下安装 xhprof 扩展: git clone https://github.com/longxinH/xhprof cd x ...

  4. P1486 [NOI2004]郁闷的出纳员

    P1486 [NOI2004]郁闷的出纳员 题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷 ...

  5. 前端如何使用easy-mock模拟接口

    1. 如何使用easy-mock // 获取 easy-mock 的模拟数据 getData () { // 开发环境使用 easy-mock 数据,正式环境使用 json 文件 if (proces ...

  6. 新建 Vue项目 使用iView报错解决

    错误如下: error in ./src/index.less Module build failed: // https://github.com/ant-design/ant-motion/iss ...

  7. linux ln链接详解

    1.序 Linux具有为一个文件起多个名字的功能,称为链接.被链接的文件可以存放在相同的目录下,但是必须有不同的文件名,而不用在硬盘上为同样的数据重复备份.另外,被链接的文件也可以有相同的文件名,但是 ...

  8. phpexcel 导入导出excel表格

    phpexcel中文实用手册 转载:http://www.cnblogs.com/freespider/p/3284828.html 下面是总结的几个使用方法 include 'PHPExcel.ph ...

  9. 天梯赛 L2-011. (二叉树) 玩转二叉树

    题目链接 题目描述 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整数. 输入格 ...

  10. (转载) 天梯赛 L2-018. 多项式A除以B

    题目链接 题目描述 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出一个非零多项式,先给出 ...