题目链接:http://codeforces.com/contest/591/problem/E

题意:有3个数字表示3个城市,每种城市都是相互连通的,然后不同种的城市不一定联通,'.'表示可以建设道路‘#’表示

不能,问最短建设多少道路能够让3种城市都联通起来

题解:直接bfs一遍所有类型的城市,bfs的同时更新第i类城市到(x,y)点的最短距离,更新第i类城市到第j类城市的距离

具体看一下代码。

#include <iostream>
#include <cstring>
#include <queue>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 1e3 + 10;
char mmp[M][M];
int n , m , dr[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1};
int dis[4][M][M] , de[4][4];//dis表示i种城市到(x,y)点的最短距离初始值为inf,de表示第i个城市到第j个城市的最短距离初始值为inf
struct TnT {
int x , y;
TnT(){}
TnT(int x , int y):x(x) , y(y) {}
};
void bfs(int num) {
queue<TnT>q;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(mmp[i][j] == num + '0') {
q.push(TnT(i , j));
dis[num][i][j] = 0;
}
}
}
while(!q.empty()) {
TnT gg = q.front();
q.pop();
char cp = mmp[gg.x][gg.y];
if(cp != '.') {
de[num][cp - '0'] = min(dis[num][gg.x][gg.y] - 1 , de[num][cp - '0']);//更新第num种到其他两种城市的距离
}
for(int i = 0 ; i < 4 ; i++) {
TnT gl = gg;
gl.x += dr[i][0] , gl.y += dr[i][1];
if(gl.x >= 0 && gl.x < n && gl.y >= 0 && gl.y < m && mmp[gl.x][gl.y] != '#') {
if(dis[num][gl.x][gl.y] == inf) {
dis[num][gl.x][gl.y] = dis[num][gg.x][gg.y] + 1;//更新第num种城市到(x,y)点的距离,这里直接更新一次就行利用了bfs的特性,好好体会一下。
q.push(gl);
}
}
}
}
}
int main() {
cin >> n >> m;
for(int i = 0 ; i < n ; i++) {
cin >> mmp[i];
}
memset(dis , inf , sizeof(dis));
memset(de , inf , sizeof(de));
for(int i = 1 ; i <= 3 ; i++) {
bfs(i);//遍历3种城市
}
int ans = de[1][2] + de[1][3];
ans = min(de[1][2] + de[2][3] , ans);
ans = min(de[1][3] + de[2][3] , ans);
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(mmp[i][j] != '.') continue;
if(dis[1][i][j] == inf || dis[2][i][j] == inf || dis[3][i][j] == inf) continue;
ans = min(dis[1][i][j] + dis[2][i][j] + dis[3][i][j] - 2 , ans);
}
}
//ans总共的情况就是要么3种城市链接起来要么在中间找一个点算一下到3种城市的距离
if(ans >= inf) cout << -1 << endl;
else cout << ans << endl;
return 0;
}

codeforces 591 E. Three States(bfs+思维)的更多相关文章

  1. codeforces 590C C. Three States(bfs+连通块之间的最短距离)

    题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...

  2. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  3. Codeforces Round #327 (Div. 2) E. Three States BFS

    E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...

  4. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  5. CodeForces 590C Three States BFS

    Three Statesy 题解: 以3个大陆为起点,都dfs一遍,求出该大陆到其他点的最小距离是多少, 然后枚举每个点作为3个大陆的路径交点. 代码: #include<bits/stdc++ ...

  6. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  7. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

  8. Codeforces 305B:Continued Fractions(思维+gcd)

    http://codeforces.com/problemset/problem/305/B 题意:就是判断 p / q 等不等于那条式子算出来的值. 思路:一开始看到 1e18 的数据想了好久还是不 ...

  9. codeforces 876 F. High Cry(思维)

    题目链接:http://codeforces.com/contest/876/problem/F 题解:一道简单的思维题,知道最多一共有n*(n+1)/2种组合,不用直接找答案直接用总的组合数减去不符 ...

随机推荐

  1. 【故障公告】发布 .NET Core 版博客站点引起大量 500 错误

    非常抱歉,今天上午的博客站点故障给大家带来了很大的麻烦,请大家谅解.这次故障是我们发布 .NET Core 版博客站点引起的,虽然我们进行了充分的准备,但还是低估了高并发下的复杂问题. 以下是故障背景 ...

  2. Flutter学习笔记(14)--StatefulWidget简单使用

    如需转载,请注明出处:Flutter学习笔记(14)--StatefulWidget简单使用 今天上班没那么忙,突然想起来我好像没StatefulWidget(有状态组件)的demo,闲来无事,写一个 ...

  3. 循环while和for

    1.循环语句的基本操作 #while循环使用,其中break是用来结束当前循环的 count = 0 while True: print(count) count += 1 if count == 3 ...

  4. lnmp环境搭建方法

    网上目前的一键搭建方法: 命令行安装: 1.源码编译安装:(个性化配置,安装配置过程繁琐) 2.使用yum或apt直接安装:(使用编译好的二进制文件安装,速度快) 3.军哥的lnmp一键脚本安装: 4 ...

  5. 【redis】redis基础命令学习集合

    写在前面 Redis是一个高速的内存数据库,它的应用十分广泛,可以说是服务端必学必精的东西.然而,学以致用,无用则无为.学了的东西必须反复的去用,去实践,方能有真知.这篇文章记录了我在redis学习过 ...

  6. git 技术栈

    之前用的都是svn ,git还是要了解的,万一哪天要用了呢

  7. Spring-boot:多模块打包

    <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...

  8. nodeJS 中mongoose操作分页

    开始前先聊聊五毛钱的: 好久没写了,可能是因为懒(哎),写这个是好事,既帮助了自己,巩固一下知识,也可以让别人给自己纠错纠错,三月份接触到了node,先是跟着一些教程写了一些小实例,感觉自己就喜欢上了 ...

  9. windows的磁盘操作之七——获取当前所有的物理磁盘号 加备注

     windows的磁盘操作之七--获取当前所有的物理磁盘号 2011-07-28 17:47:56 标签:windows API DeviceIoControl 物理磁盘 驱动器号 原创作品,允许转载 ...

  10. Android Studio和 adb 的一些常用技巧

    AS和ADB的随身手册 工欲善其事,必先利其器. 最近因为换了Mac,很多地方有些不太适应,刚好最近有想写一篇记录一些小工具技巧的文章,顺便就把Mac中AS常用的快捷键也一并对应记录起来吧. 以下为A ...