原题: 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的更多相关文章

  1. 2014 Super Training #4 D Paint the Grid Again --模拟

    原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结 ...

  2. ZOJ 3781 Paint the Grid Reloaded 连通块

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...

  3. Paint the Grid Reloaded ZOJ - 3781 图论变形

    Paint the Grid Reloaded Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %ll ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【最短路+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 ...

  9. 2014 Super Training #8 B Consecutive Blocks --排序+贪心

    当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...

随机推荐

  1. sqlite3之基本操作(一)

    简单的介绍 SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经 ...

  2. 使用PowerQuery操作OData数据

             Excel是我们耳熟的办公软件.PowerQuery是一个允许连接多种数据源的Excel插件.它能从一个网页上智能查询数据.使用PowerQuery能合并数据集使用join,merg ...

  3. linux多线程-互斥&条件变量与同步

    多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...

  4. eclipse:File->New没有Android Application Project的解决办法

    我的Eclipse版本是:Kepler Service Release 1,截图: 解决步骤: 1.单击Window,选择Customize Perspective,如图: 2.勾选Android A ...

  5. RHEL7虚拟机实验快照

    配置虚拟机连接网络 首先确保NetworkManager服务正常运行 [root@administrator ~]# systemctl status NetworkManager ● Network ...

  6. ASP.NET检测到有潜在危险的 Request.Form 值解决方案汇总

    ASP.NET检测到有潜在危险的 Request.Form 值解决方案汇总 当我们在网站中使用CKEditor等富文本编辑器时,大多都会遇到这样的到警告 这是因为ASP.NET默认开启对页面提交内容的 ...

  7. 3.0之后在LinearLayout里增加分割线

    android:divider="@drawable/shape"<!--分割线图片--> android:showDividers="middle|begi ...

  8. 操作集合的工具类:Collections

    Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对集合对象实现同步控制等方法 ...

  9. Android 在外部存储读写文件

    本文主要介绍android中如何在外部存储读写数据 sd卡的路径 sdcard:2.3之前的sd卡路径 mnt/sdcard:4.3之前的sd卡路径 storage/sdcard:4.3之后的sd卡路 ...

  10. IOS客户端Coding项目记录(五)

    1:统一修改导航栏的样式,在 AppDelegate.m中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWit ...