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重启命令
			
linux平台及windows平台mysql重启方法 Linux下重启MySQL的正确方法: 1.通过rpm包安装的MySQL service mysqld restart 2.从源码包安装的MySQ ...
 - Error writing file‘frm‘(Errcode: 28)
			
Error writing file‘frm‘(Errcode: 28) mysql出现这个错误,表示磁盘已经满了,该增加容量了.
 - Sass学习之路(1)——Sass简介
			
Sass是CSS的一种预处理器语言,类似的语言还有Less,Stylus等. 那么什么是CSS预处理器? CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些 ...
 - 【原】IOS中KVO模式的解析与应用
			
最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单 ...
 - OC中NSArray
			
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
 - setSupportActionBar(toolbar)导致程序崩溃闪退
			
最近在做一个项目,使用了第三方的开源项目,主要是想实现android5.0之后推出的MaterialDesign的风格,但是代码已经写好了,发现一运行就闪退,所以就开始debug,发现问题出现在 To ...
 - Java 实现多线程的三种方式
			
import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; public class Main { pu ...
 - const,static,extern简介(重要)
			
一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 编译时刻:宏是预编译(编译之前处理),const是编译 ...
 - PMP 项目管理过程组与知识领域
 - WebService核心文件【server-config.wsdd】详解及调用示例
			
WebService核心文件[server-config.wsdd]详解及调用示例 作者:Vashon 一.准备工作 导入需要的jar包: 二.配置web.xml 在web工程的web.xml中添加如 ...