题意:

      给你一个n*m的矩阵,上面只有两种字符,X或者O,每次可以同时改变相同颜色的一个连通块,上下左右连通才算连通,用最小的步数把这个图弄成全是X或者全是O,题意要是没看懂看下面的样例。

Sample Input

2

2 2

OX

OX

3 3

XOX

OXO

XOX

Sample Output

1

2

Hint

For the second sample, one optimal solution is:

Step 1. flip (2, 2)

XOX

OOO

XOX

Step 2. flip (1, 2)

XXX

XXX

XXX

思路:

      这个可以用最短路来做(也可以直接广搜,因为是在格子上找距离),首先我们要把每一个连通块缩成一个点,然后把相邻的联通快之间建边,然后枚举每一个点为起点,跑单源最短路,然后对于每一个点的当前答案就是所有点中离他最远的那个,最后在在每一个最远的点钟找到最小的那个,为什么这么做是对的,我们就假设我们枚举的最短路的起点是中心点,我们从中心点往外扩,每一次绝对可以扩展一层,这

一层有两种扩展方式,改变中间或者改变要扩展的这层,画一下就理解了。。


#include<stdio.h>
#include<string.h>
#include<queue>
#include<map> #define N_node 1600 + 100
#define N_edge 2000000
#define INF 100000000 using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; STAR E[N_edge];
int list[N_node] ,tot;
int s_x[N_node];
int mapp[50][50] ,n ,m;
int mk[50][50];
int dir[4][2] = {0 ,1 ,0 ,-1 ,1 ,0 ,-1 ,0};
map<int ,map<int ,int> >kk; void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
} int spfa(int s ,int n)
{
for(int i = 0 ;i <= n ;i ++)
s_x[i] = INF;
int mark[N_node] = {0};
mark[s] = 1;
s_x[s] = 0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int xin ,tou;
tou = q.front();
q.pop();
mark[tou] = 0;
for(int k = list[tou] ;k ;k = E[k].next)
{
xin = E[k].to;
if(s_x[xin] > s_x[tou] + E[k].cost)
{
s_x[xin] = s_x[tou] + E[k].cost;
if(!mark[xin])
{
mark[xin] = 1;
q.push(xin);
}
}
}
}
int now = 0;
for(int i = 1 ;i <= n ;i ++)
if(now < s_x[i]) now = s_x[i];
return now;
} bool ok(int x ,int y ,int ys)
{
return x >= 1 && x <= n && y >= 1
&& y <= m && !mk[x][y]
&& mapp[x][y] == ys; } void DFS(int x ,int y ,int now ,int ys)
{
for(int i = 0 ;i < 4 ;i ++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(ok(xx ,yy ,ys))
{
mk[xx][yy] = now;
DFS(xx ,yy ,now ,ys);
}
}
return ;
} int main ()
{
int i ,j ,t;
char str[50];
scanf("%d" ,&t);
while(t --)
{
scanf("%d %d" ,&n ,&m);
for(i = 1 ;i <= n ;i ++)
{
scanf("%s" ,str);
for(j = 1 ;j <= m ;j ++)
mapp[i][j] = str[j-1] == 'X';
}
memset(mk ,0 ,sizeof(mk));
int now = 0;
for(i = 1 ;i <= n ;i ++)
for(j = 1 ;j <= m ;j ++)
{
if(mk[i][j]) continue;
mk[i][j] = ++now;
DFS(i ,j ,now ,mapp[i][j]);
}
kk.clear();
memset(list ,0 ,sizeof(list)) ,tot = 1;
for(i = 1 ;i <= n ;i ++)
for(j = 1 ;j <= m ;j ++)
{
if(i > 1)
{
if(mk[i][j] != mk[i-1][j] && !kk[mk[i][j]][mk[i-1][j]])
{
kk[mk[i][j]][mk[i-1][j]] = 1;
add(mk[i][j] ,mk[i-1][j] ,1);
}
}
if(i < n)
{
if(mk[i][j] != mk[i+1][j] && !kk[mk[i][j]][mk[i+1][j]])
{
kk[mk[i][j]][mk[i+1][j]] = 1;
add(mk[i][j] ,mk[i+1][j] ,1);
}
}
if(j > 1)
{
if(mk[i][j] != mk[i][j-1] && !kk[mk[i][j]][mk[i][j-1]])
{
kk[mk[i][j]][mk[i][j-1]] = 1;
add(mk[i][j] ,mk[i][j-1] ,1);
}
}
if(j < m)
{
if(mk[i][j] != mk[i][j+1] && !kk[mk[i][j]][mk[i][j+1]])
{
kk[mk[i][j]][mk[i][j+1]] = 1;
add(mk[i][j] ,mk[i][j+1] ,1);
}
}
}
int ans = INF;
for(i = 1 ;i <= now ;i ++)
{
int tmp = spfa(i ,now);
if(ans > tmp) ans = tmp;
}
printf("%d\n" ,ans);
}
return 0;
}

ZOJ 3781 最短路(想法好题目)的更多相关文章

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

  2. Zoj 3781(构造)

    Zoj 3781(构造) Zoj 3781 As we all know, Coach Gao is a talented chef, because he is able to cook M dis ...

  3. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

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

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

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

  7. ZOJ 3781 Paint the Grid Reloaded 连通块

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

  8. ZOJ - 3781 Paint the Grid Reloaded 题解

    题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...

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

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

随机推荐

  1. System.Net.Mail邮件发送抄送附件(多个)

    /// <summary> /// 邮件发送抄送附件 /// </summary> /// <param name="mailTo">收件人(可 ...

  2. 十一. SpringCloud Alibaba

    1. SpringCloud Alibaba简介 1.1 为什么会出现SpringCloud Alibaba Spring Cloud Netflix项目进入到维护模式 什么是维护模式?=> 将 ...

  3. 翻译:《实用的Python编程》04_02_Inheritance

    目录 | 上一节 (4.1 类) | 下一节 (4.3 特殊方法) 4.2 继承 继承(inheritance)是编写可扩展程序程序的常用手段.本节对继承的思想(idea)进行探讨. 简介 继承用于特 ...

  4. [unknown source] 整数拆分

    一.题目 题目描述 定义一个整数拆分序列 \(a\) 的权值为: \[\sum_{i=1}^n\sum_{j=1}^{i-1}\gcd(a_i,a_j) \] 求对于一个整数 \(n\) 所有整数拆分 ...

  5. malloc和free解析

    malloc和free都是库函数,调用系统函数sbrk()来分配内存.除了分配可使用的内存以外,还分配了"控制"信息,这有点像内存池常用的手段.并且,分配的内存是连续的. 1. m ...

  6. Nodejs学习笔记(2) 阻塞/非阻塞实例 与 Nodejs事件

    1. Node.js异步编程的特点 2. 阻塞与非阻塞的实例 2.1 阻塞代码实例 2.2 非阻塞代码实例 3. Node.js的事件驱动 4. 事件循环实例 1. Node.js异步编程的特点 参考 ...

  7. 攻防世界 reverse 进阶 APK-逆向2

    APK-逆向2 Hack-you-2014 (看名以为是安卓逆向呢0.0,搞错了吧) 程序是.net写的,直接祭出神器dnSpy 1 using System; 2 using System.Diag ...

  8. IgniteMe -高校网络信息安全运维挑战赛

    1 int __cdecl main(int argc, const char **argv, const char **envp) 2 { 3 void *v3; // eax 4 int v4; ...

  9. 12、MyBatis教程之缓存

    13.缓存 简介 1.什么是缓存 [ Cache ]? 存在内存中的临时数据. 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高 ...

  10. [go-linq]-Go的.NET LINQ式查询方法

    关于我 我的博客|文章首发 开发者的福音,go也支持linq了 坑爹的集合 go在进行集合操作时,有很不舒服的地方,起初我真的是无力吐槽,又苦于找不到一个好的第三方库,只能每次写着重复代码.举个栗子 ...