点击打开链接

先把每个连通块都当做一个点 然后建图 然后对每一个点bfs得到一个最大深度 再取最小值

这样做是因为这个图是个二分图 每个点与其邻接点的颜色都不一样

#include <bits/stdc++.h>
using namespace std;
#define N 0x3f3f3f3fstruct node
{int v;int next;
};queue <int> que;
node edge[100000];
int book[50][50],e[2000][2000];
int first[2000],dis[2000];
int n,m,num,cnt;
char mp[50][50];void dfs(int x,int y,char tp)
{int next[4][2]={0,-1,-1,0,0,1,1,0};int i,tx,ty;for(i=0;i<4;i++){tx=x+next[i][0];ty=y+next[i][1];if(tx<0||tx>n-1|ty<0||ty>m-1||book[tx][ty]!=0||mp[tx][ty]!=tp) continue;book[tx][ty]=cnt;dfs(tx,ty,tp);}return;
}void addedge(int u,int v)
{edge[num].v=v;edge[num].next=first[u];first[u]=num++;return;
}void build()
{int next[4][2]={0,-1,-1,0,0,1,1,0};int i,j,k,tx,ty;memset(e,0,sizeof(e));memset(first,-1,sizeof(first));num=0;for(i=0;i<n;i++){for(j=0;j<m;j++){for(k=0;k<4;k++){tx=i+next[k][0];ty=j+next[k][1];if(tx<0||tx>n-1||ty<0||ty>m-1) continue;if(book[i][j]!=book[tx][ty]&&!e[book[i][j]][book[tx][ty]]){e[book[i][j]][book[tx][ty]]=e[book[tx][ty]][book[i][j]]=1;addedge(book[i][j],book[tx][ty]);addedge(book[tx][ty],book[i][j]);}}}}return;
}int bfs(int s)
{int i,u,v,res;while(!que.empty()) que.pop();memset(dis,-1,sizeof(dis));que.push(s);dis[s]=0;res=0;while(!que.empty()){u=que.front();que.pop();for(i=first[u];i!=-1;i=edge[i].next){v=edge[i].v;if(dis[v]==-1){dis[v]=dis[u]+1;res=max(res,dis[v]);que.push(v);}}}return res;
}int main()
{int t,i,j,ans;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=0;i<n;i++){scanf("%s",mp[i]);}memset(book,0,sizeof(book));cnt=0;for(i=0;i<n;i++){for(j=0;j<m;j++){if(!book[i][j]){cnt++;book[i][j]=cnt;dfs(i,j,mp[i][j]);}}}build();ans=N;for(i=1;i<=cnt;i++){ans=min(ans,bfs(i));}printf("%d\n",ans);}return 0;
}

Paint the Grid Reloaded ZOJ - 3781相关推荐

  1. *【ZOJ - 3781】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 initi ...

  2. ZOJ 3781 Paint the Grid Reloaded

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

  3. 【ZOJ - 3780】Paint the Grid Again(拓扑排序,图论,证明性质)

    题干: Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black ...

  4. ZOJ 3781 最短路(想法好题目)

    题意:       给你一个n*m的矩阵,上面只有两种字符,X或者O,每次可以同时改变相同颜色的一个连通块,上下左右连通才算连通,用最小的步数把这个图弄成全是X或者全是O,题意要是没看懂看下面的样例. ...

  5. The 11th Zhejiang Provincial Collegiate Programming Contest

    Problem Arrangement(状态压缩+递推) http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:输入 ...

  6. problem solving automation

    分工一定要明确啊啊啊啊啊 Nero 数据结构  图论   DP   博弈 JayYe  数论   DP   模拟搜索 博弈  wuyiqi 数论  数据结构   图论 字符串 模拟搜索  几何  20 ...

  7. ZOJ 2747 Paint the Wall(离散化+暴力)题解

    题意:给你一个面,然后涂颜色,问你最后剩多少颜色,每种颜色面积. 思路:第一反应是二维线段树,代码又臭又长,可以做.但是这题暴力+离散化就可以过.可以看到他给的n只有100,也就是说最坏情况下会涂10 ...

  8. [转]通过创建一个位图的XY Chart来学习Android绘图类Rect,Paint,Bitmap,Canvas(附源码)...

    本文转自:http://www.cnblogs.com/salam/archive/2010/11/10/1873437.html 绘制一个XY集是一种很常见的任务,基于Android平台的绘制很简单 ...

  9. Grid Coloring(AtCoder-2687)

    Problem Description We have a grid with H rows and W columns of squares. Snuke is painting these squ ...

最新文章

  1. STM32使用GPIO_WriteBit()函数使LED灯闪烁
  2. Linux下的USB总线驱动 1
  3. STM32F7xx —— 输入
  4. 关于JavaBean
  5. @初学编程的朋友们,如果你能学得这些方法,学习将会更快一步!
  6. 新的恶意软件将后门植入微软 SQL Server 中
  7. 基于JAVA+JSP+MYSQL的小说网站阅读管理系统
  8. 学会用这个工具做分析,1年积累3年工作经验
  9. Git:常用的命令(不断迭代完善)
  10. ios显示gif格式图片
  11. mybatis自动生成service、dao、mapper
  12. c语言小树苗健康指数,一颗特别的小树苗.doc
  13. 处于停机等非正常状态_设备非正常停机管理指导办法
  14. Python 日历模块 calendar
  15. FIL最新行情,FIL预测能涨至750U是否过于夸大?会突破新高吗?
  16. [Linux] ntpdate slew模式调整时间
  17. 劳动仲裁委员会的具体地址即(朝阳区酒仙桥南十里居28楼的具体路线)______转...
  18. 【视频】主成分分析PCA降维方法和R语言分析葡萄酒可视化实例|数据分享
  19. java 多线程 压缩_Java 多线程拷贝文件夹并调用tinyPng算法接口压缩图片实现(生产消费变种)...
  20. 如何下载Direct3D9Ex

热门文章

  1. 集美大学计算机科学与技术知乎,集美大学
  2. android的多开器解析和检测实现
  3. Leetcode高频算法题——字节跳动篇
  4. FEKO仿真涂覆目标时的参数设置
  5. feko 2017安装包
  6. 级联H桥statcom,级联H桥SVG,级联H桥静止同步补偿器,级联H桥静止无功发生器
  7. 纯js实现点击预览图片效果
  8. uniapp 下拉刷新
  9. Flash实用代码总汇
  10. oracle 数据库表结构对比