题意:

      给你一个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. HoloWAN在连接路由器时应该选择WAN口还是LAN口,有什么区别?

    HoloWAN在连接路由器时应该选择WAN口还是LAN口,有什么区别? 在解决问题前,需要连接到,路由器的WAN口和LAN口的作用不同. WAN口是对外的接口,连接广域网.当联网设备和路由器都开启了D ...

  2. Linux速通07 硬盘分区、格式化及文件系统管理

    硬件设备与文件名的对应关系 # 在Linux系统中,每个设备都被当作一个文件来对待 # 各种设备在Linux中的文件名 设备 设备在Linux内的文件名 IDE硬盘 /dev/hd[a-d] SCSI ...

  3. SEO 在 SPA 站点中的实践

    背景 观察基于 create-react-doc 搭建的文档站点, 发现网页代码光秃秃的一片(见下图).这显然是单页应用 (SPA) 站点的通病 -- 不利于文档被搜索引擎搜索 (SEO). 难道 S ...

  4. Go语言|类型转换和类型别名

    类型转换 同类型之间的转换 Go语言中只有强制类型转换,没有隐式类型转换.该语法只能在两个类型之间支持相互转换的时候使用. import "fmt" func main() { v ...

  5. 热门跨平台方案对比:WEEX、React Native、Flutter和PWA

    本文主要对WEEX.React Native.Flutter和PWA几大热门跨平台方案进行简单的介绍和对比.内容选自<WEEX跨平台开发实战> (WEEX项目负责人力荐,从入门到实战,教你 ...

  6. MySQL按天备份二进制日志

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:guozhen.zhang     import MySQLdbimport timeimp ...

  7. 某SQL注入--报错注入payload

    1.证明存在sql注入,根据这个报错语句,,有'  有% 2.payload  闭合语句 %' or (select extractvalue("anything",concat( ...

  8. mysql建表约束

    --mysql建表约束--主键约束它能够唯一确定一张表中的内容,也就是我们通过某个字段添加约束,就可以是的该字段唯一(不重复)且不为空.create table  user(    id int pr ...

  9. Anchor-Free总结

    目录 Anchor-Free综述 一. CornerNet 1.1 概述 1.2 模块介绍 1.2.1 Heatmap 1.2.2 Offset 1.2.3 Grouping Corners 1.2. ...

  10. IPFS挖矿硬盘满了会怎样?

    IPFS是一个互联网协议,对标现在的HTTP.所以,可以想见未来IPFS有多大的价值.所谓IPFS挖矿,是基于IPFS,挖的是filecoin,称其为filecoin挖矿倒是更为贴切.许多初接触IPF ...