枚举,$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. 我的CCF备考指南

    CCF计算机软件能力认证(简称CCF CSP认证). 认证涉及知识点: 认证内容主要覆盖大学计算机专业所学习的程序设计.数据结构.算法以及相关的数学基础知识.包括但不限于: (1)程序设计基础 逻辑与 ...

  2. 2015/9/5 Python基础(9):条件和循环

    条件语句Python中的if语句如下: if expression: expr_true_suite 其中expression可以用布尔操作符and, or 和 not实现多重判断条件.如果一个复合语 ...

  3. MSSQL Procudure Sample

    代码: USE [Internal_Timesheet] GO /****** Object: StoredProcedure [dbo].[ManageTSReminder] Script Date ...

  4. 快速排序Quick sort

    快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...

  5. web版canvas做飞机大战游戏 总结

    唠唠:两天的时间跟着做了个飞机大战的游戏,感觉做游戏挺好的.说是用html5做,发现全都是js.说js里一切皆为对象,写的最多的还是函数,都是函数调用.对这两天的代码做个总结,希望路过的大神指点一下, ...

  6. LTC 钱包部署

    基础环境 系统: CentOS 7.x nodejs: v4.6.0 zeromq: 4.x 安装nodejs + zeromq 基础依赖 yum install -y gcc make gcc-c+ ...

  7. 转一篇sublime必备的一些插件

    Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/installatio ...

  8. Apache2.4+Tomcat7 集群

    Apache2.4+Tomcat7 集群: 1.下载并安装相对应的软件 apache下载地址:http://httpd.apache.org/ 这里使用apache2.4 tomcat下载地址:htt ...

  9. 随机森林(Random Forest)详解(转)

    来源: Poll的笔记 cnblogs.com/maybe2030/p/4585705.html 1 什么是随机森林?   作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Fores ...

  10. Perl6 Bailador框架(7):模版编写

    先看一个例子: use v6; use Bailador; my $data = ' <form action="", method="get"> ...