2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS
原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781
题意:
给一个n*m的X,O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X->O或O->X),问给定的矩阵最少操作多少次可以全部变成一样的颜色。
网上思路:
每次操作都将本身所在的连通块与和自己相邻的不同颜色的连通块变成同一种颜色,也就是变成一个连通块了,那么要使n次操作后全部变成一样的颜色,也就是从某点出发到达其余所有点。所以先dfs把连通块缩成点,然后相邻的连通块之间建边,枚举以每个点为根的情况,bfs求出每种情况的深度,取最小的即为答案。
思路很重要,实现起来不难。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#define Mod 1000000007
using namespace std;
#define N 44 char ss[N][N];
int ind[N][N],vis[N][N];
int now,n,m;
int inq[];
int dx[] = {,,-,};
int dy[] = {,,,-};
vector<int> G[]; struct node
{
int dis,ind;
}; int OK(int nx,int ny)
{
if(nx < n && nx >= && ny < m && ny >= )
return ;
return ;
} void dfs(int nx,int ny,int now)
{
for(int k=;k<;k++)
{
int kx = nx + dx[k];
int ky = ny + dy[k];
if(!OK(kx,ky))
continue;
if(ss[kx][ky] == ss[nx][ny])
{
if(ind[kx][ky] == -)
{
ind[kx][ky] = now;
dfs(kx,ky,now);
}
}
else if(ind[kx][ky] != -) //已经有标号,连边
{
int v = ind[kx][ky];
G[v].push_back(now);
G[now].push_back(v);
}
}
} int SPFA(int num)
{
queue<node> que;
memset(inq,,sizeof(inq));
node S,tmp,now;
S.dis = ;
S.ind = num;
int res = ;
que.push(S);
inq[num] = ;
while(!que.empty())
{
tmp = que.front();
que.pop();
res = max(res,tmp.dis);
now.dis = tmp.dis + ;
for(int i=;i<G[tmp.ind].size();i++)
{
now.ind = G[tmp.ind][i];
if(!inq[now.ind])
{
inq[now.ind] = ;
que.push(now);
}
}
}
return res;
} int main()
{
int i,j,k;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
scanf("%s",ss[i]);
now = ;
memset(ind,-,sizeof(ind));
for(i=;i<=n*m;i++)
G[i].clear();
for(i=;i<n;i++)
for(j=;j<m;j++)
if(ind[i][j] == -)
{
ind[i][j] = now;
dfs(i,j,now);
now++;
}
int ans = Mod;
for(i=;i<now;i++)
ans = min(ans,SPFA(i));
printf("%d\n",ans);
}
return ;
}
2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS的更多相关文章
- 2014 Super Training #4 D Paint the Grid Again --模拟
原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结 ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- 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 ...
- 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 initially ...
- 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 ...
- 【最短路+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 ...
- 2014 Super Training #8 B Consecutive Blocks --排序+贪心
当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...
随机推荐
- php学习笔记:foreach循环访问关联数组里的值
foreach循环可以将数组里的所有值都访问到,下面我们展示下,用foreach循环访问关联数组里的值. 例如: $fruit=array('apple'=>"苹果",'ba ...
- 深入浅出java 8 lambda表达式--零基础一分钟入门
lambda从使用上来说,第一感觉直白的理解就是,少了很多不必要的匿名回调类的写法,比如: public static void main(String[] args) { PlatformQuery ...
- Android笔记——Windows环境下Android Studio v1.0安装教程
本文主要讲解Windows环境下Android Studio的安装教程,Mac的Android Studio安装与此类似不在赘述,另外友情提示Windows下的SDK与Mac的SDK是通用的,可以直接 ...
- SilverLight MD5加密
效果体验:http://keleyi.com/tool/md5.htm 嵌入页面的代码: <div style="width:400px;height:230px"> ...
- React入门--------组件API
setState 参数:nextState(object),[callback(function)] 设置nextState的某个键值.通常如果希望在某个事件或某个回调中来重新渲染组件,setStat ...
- 得到设备是何种iPhone设备 + 怎么获得启动页面图片
一.前言 今天做一个功能,需要动态的获得启动页,然后根据不同设备去使用不用的启动页图片. 二.正文 常规来说,我们直接判断是何种设备,然后通过name去获得图片选择性加载即可.但是实际上遇到的两个问题 ...
- Struts2(十二)使用验证框架验证数据较验
一.数据验证 1.1.为什么要进行数据验证 对数据的合法性进行检查,只允许合法的数据进入应用程序 1.2.在哪里实现数据验证 客户端验证: 数据提交前在客户端验证 可使用JavaScript或者JQu ...
- 自定义Dialog以及Dialog返回值到Activity
步骤: 1.定义自定义的Dialog的布局文件 2.写一个类MyDialog继承Dialog 3.Dialog 返回值到Activity的方法是定义一个接口,接口中定义返回值到Activity的方法, ...
- Effective Java 13 Minimize the accessibility of classes and members
Information hiding is important for many reasons, most of which stem from the fact that it decouples ...
- 【mysql】一个关于order by排序的问题
I have a table CREATE TABLE `tableMain` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value1` varchar(45) ...