ZOJ 3781 Paint the Grid Reloaded
枚举,$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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- 【最短路+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 ...
- 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 ...
随机推荐
- Chromedriver 的安装与配置
首先是下载网址:https://sites.google.com/a/chromium.org/chromedriver/downloads(需要FQ,用Browser浏览器即可翻进,版本要和Chro ...
- C11内存管理之道:智能指针
1.shared_ptr共享智能指针 std::shared_ptr使用引用计数,每个shared_ptr的拷贝都指向相同的内存,在最后一个shared_ptr析构的时候,内存才会释放. 1.1 基本 ...
- concurrent.futures 使用及解析
from concurrent.futures import ThreadPoolExecutor, as_completed, wait, FIRST_COMPLETED from concurre ...
- 快速搭建 DNS 服务器: skydns + etcd
参考: [ skynetservice github ] [ skydns 测试记录 CSDN ] etcd 安装配置 安装 yum install etcd 配置 sed -i 's@ETCD_LI ...
- bzoj 1050 并查集
先按边长排序,假设s与t连通,那么我们可以枚举s与t的路径中最短的一条边,通过类似与kruskal的方法找到s与t的路径在当前最小边权情况下尽量小的最大边权,用这个比值更新答案. 特别的,我们对于某一 ...
- ie8下input文字偏上select文字偏下
1.ie8下input文字偏上 正常情况下input的显示情况如下 当设置input的高度时,就会出现文字不垂直居中偏上的情况,如图 解决方案 强input的行高line-height与其高度设置一致 ...
- [bzoj1070] 修车
这周学习了费用流,就写了几题.其中有一题就是bzoj上的修车,看起来很丧,交了6次都是除了样例全wa(事实证明样例说明不了什么,还会误导你……). 题目大意:有m个技术人员n辆车,一个技术人员只能同时 ...
- Perl6 Bailador框架(5):利用正则匹配路径
use v6; use Bailador; =begin pod 我们在路径设置上, 可以利正则表达式捕获的字符串作为子例程参数 =end pod get '/perl6/(.+)' => su ...
- python基础===【字符串】所有相关操作
#字符串的相关操作 #基本操作 #+ 字符串连接操作 str1 = '来是come走是go' str2 = '点头yes摇头no' result = str1 + str2 print(result) ...
- udpserver.pl 和 udpclient.pl
udpserver.pl #!use/bin/perl -w use Socket; #导入Socket库 ,INADDR_ANY);#压入sockaddr_in模式,利用了全局当地压缩地点INADD ...