枚举,$BFS$,连通块缩点。

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

#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std; int T,n,m;
char s[][];
int Belong[][],block,use[*];
int dx[]={,,-,};
int dy[]={,-,,}; int h[*];
struct Edge
{
int from,to,nx;
}e[];
int sz; bool ok(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m) return ;
return ;
} void dfs(int x,int y)
{
Belong[x][y]=block;
if(ok(x-,y)&&s[x][y]==s[x-][y]&&Belong[x-][y]==) dfs(x-,y);
if(ok(x+,y)&&s[x][y]==s[x+][y]&&Belong[x+][y]==) dfs(x+,y);
if(ok(x,y-)&&s[x][y]==s[x][y-]&&Belong[x][y-]==) dfs(x,y-);
if(ok(x,y+)&&s[x][y]==s[x][y+]&&Belong[x][y+]==) dfs(x,y+);
} void add(int x,int y)
{
e[sz].from=x; e[sz].to=y; e[sz].nx = h[x];
h[x]=sz++;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) scanf("%s",s[i]); memset(Belong,block=,sizeof Belong);
sz=;
memset(h,0xff,sizeof(h));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(Belong[i][j]) continue;
block++; dfs(i,j);
}
} for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
for(int k=;k<;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(!ok(x,y)) continue;
if(Belong[i][j]==Belong[x][y]) continue;
add(Belong[i][j],Belong[x][y]);
}
}
} int ans=0x7fffffff;
for(int i=;i<=block;i++)
{
memset(use,-,sizeof(use));
int mx=;
use[i]=;
queue<int>q;
q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int j=h[x];j!=-;j=e[j].nx)
{
int y=e[j].to;
if(use[y]!=-) continue;
use[y]=use[x]+;
mx=max(use[y],mx);
q.push(y);
}
}
ans=min(mx,ans);
}
printf("%d\n",ans);
}
return ;
}

ZOJ 3781 Paint the Grid Reloaded的更多相关文章

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

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

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

  4. ZOJ 3781 Paint the Grid Reloaded 连通块

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

  5. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  6. ZOJ - 3781 Paint the Grid Reloaded 题解

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

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

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

  8. 【最短路+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 ...

  9. ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

    Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cel ...

随机推荐

  1. Beautiful Soup的一些中文资料

    如果你着急用的话,可以看下这个简略版的,非常简单: 转自  人世间http://rsj217.diandian.com/post/2012-11-01/40041235132 当然,强烈推荐你看一下的 ...

  2. maven工程pom.xml报Missing artifact net.sf.jasperreports:jasperreports:jar:6.2.0

    有时maven工程的pom.xml报以下类型错误: Description Resource Path Location TypeMissing artifact net.sf.jasperrepor ...

  3. [LeetCode] 4. Median of Two Sorted Arrays ☆☆☆☆☆

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  4. MyBatis框架的使用及源码分析(一) 配置与使用

    我们先来看一个例子,简单的了解一下mybatis的mapper接口方式的使用. package org.mybatis.spring.sample; import org.apache.ibatis. ...

  5. jquery 遮罩层显示img

    如果点击iframe中的image显示整个页面的遮罩层,可参考如下: http://blog.csdn.net/shiaijuan1/article/details/70160714 具体思路就是,顶 ...

  6. 【Foreign】登山 [DP][数学]

    登山 Time Limit: 10 Sec  Memory Limit: 256 MB Description 恶梦是一个登山爱好者,今天他来到了黄山 俗话说的好,不走回头路.所以在黄山,你只能往前走 ...

  7. Spring 框架的设计理念与设计模式分析(山东数漫江湖)

    Spring 的骨骼架构 Spring 总共有十几个组件,但是真正核心的组件只有几个,下面是 Spring 框架的总体架构图: 图 1 .Spring 框架的总体架构图 从上图中可以看出 Spring ...

  8. jq_常用开发模块

    1.循环模块 var html = ""; $.each(data, function(k, v) { html += '<li>' + '<div>' + ...

  9. Apache的Commons Lang和BeanUtils

    1.字符串的空判断 //isEmpty System.out.println(StringUtils.isEmpty(null));      // true System.out.println(S ...

  10. Runas replacement tool

    1. RunAsSpc Runas 无法在脚本中输入密码,可以使用RunAsSpc替代. RunAsSpc = runas + password + encryption https://robotr ...