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 ...
随机推荐
- Spring 学习笔记之整合Hibernate
Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQ ...
- 【BZOJ】2200: [Usaco2011 Jan]道路和航线
[题意]给定n个点的图,正权无向边,正负权有向边,保证对有向边(u,v),v无法到达u,求起点出发到达所有点的最短距离. [算法]拓扑排序+dijkstra [题解]因为有负权边,直接对原图进行spf ...
- HDU 1002 A + B Problem II (大数加法)
题目链接 Problem Description I have a very simple problem for you. Given two integers A and B, your job ...
- 如何用js自己实现Animate运动函数
js运动是我们学习js必不可少的研究部分,首先我们要知道js的运动其实仅仅是不断改变元素的某个属性值而已,比如不断改变一个绝对定位div的left值,那么你看到的效果就是这个div不断的向右边运动,那 ...
- bzoj 3197 DP
这道题我们可以看成给定两个黑白树,可以修改其中一棵树的颜色,问最少修改多少颜色可以使两棵树同构. 首先我们知道在树的同构中树上最长链中点(如果是偶数的话就是中间两个点)是不变的,我们把这个点叫做树的重 ...
- RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.
今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency d ...
- CiteSeer统计的计算机领域的期刊和会议的影响因子(2005)
产生自CiterSeer 2005数据库,实际的影响因子可能更高.仅供参考使用.真实的IF还需去官网查看 . OSDI: 3.31 (top 0.08%) . USENIX Symposium on ...
- Yii 1.1.17 二、Gii创建后台与后台登录验证
一.用Gii创建后台模块 1.启用gii,在config/main.php 'gii' => array( 'class' => 'system.gii.GiiModule', 'pass ...
- 剑指offer中数据结构与算法部分学习
2.3.4 树 遍历:前中后序,宽度优先. 二叉树的特例:二叉搜索树.堆(最大堆和最小堆,用于找最值).红黑树(c++ STL中的很多数据结果就是基于这实现的): 题7-重建二叉树:递归,设置四个位点 ...
- Unsupported gpu architecture 'compute_20'
NVCC src/caffe/layers/reduction_layer.cunvcc fatal : Unsupported gpu architecture 'compute_20'Make ...