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 其实就 ...
随机推荐
- mysql innodb表 utf8 gbk占用空间相同,毁三观
昨天因为发生字符集转换相关错误,今天想验证下utf8和gbk中英文下各自空间的差距.这一测试,绝对毁三观,无论中文还是中文+英文,gbk和utf8占用的实际物理大小完全相同,根本不是理论上所述的“UT ...
- 每一个成功的程序员的身后都有一个--------Parse
相信好多同行都用过Parse,而正是因为Parse给我们的开发带来的极大的便利,才有了项目从零开始,到正式上线仅仅用上不到两周的时间,现在Swift还在迅速的发展,很快就会占有大量的市场,现在就就结合 ...
- winform(MDI窗体容器、权限设置)
一.MDI窗体容器: 1.功能: 它可以让其它窗体在它的内部打开,无法超出它的范围 将某个窗体的属性:IsMdiContainer设置为true - 窗口样式 2.问题: (1)如何将其它窗体在它的内 ...
- [Tool] 透过PowerPoint Online在部落格文章里内嵌简报
[Tool] 透过PowerPoint Online在部落格文章里内嵌简报 前言 讲课的时候,用PowerPoint做简报,好像已经成了讲课的惯例.而在课后,将课堂简报整理成部落格的文章,如果单纯是在 ...
- PHP PEAR2
出错: Pyrus\Installer\Exception: Installation failed Pyrus\AtomicFileTransaction\MultiException: Unabl ...
- 函数改变全局变量-JS
切记,一定按三步走: 1. 全局变量声明 2. 函数声明 3. 函数调用 正确做法: var dataStr = null; function remoteCallback(data) { dataS ...
- SharePoint 2013 术语和术语集介绍
托管元数据是一个集中管理的术语的分层集合,我们可以定义术语和术语集,然后将其用作 SharePoint Server 2013 中项目的属性.简单的说,术语是一个可与 SharePoint Serve ...
- hadoop2.6完全分布式安装HBase1.1
本文出自:http://wuyudong.com/archives/119 对于全分布式的HBase安装,需要通过hbase-site.xml文档来配置本机的HBase特性,由于各个HBase之间通过 ...
- 在Android开发中使用Ant 一:环境的搭建及入门
配置Ant环境 下载Ant:http://ant.apache.org/bindownload.cgi 在windows上应该选择zip压缩包,将zip压缩包解压到一个目录. 打开系统环境变量,在系统 ...
- UINavigationBar 和 UINavigationItem的属性设置
#import "RootViewController.h" @interface RootViewController () @end @implementation RootV ...