codeforces 591 E. Three States(bfs+思维)
题目链接: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+思维)的更多相关文章
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 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 ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- CodeForces 590C Three States BFS
Three Statesy 题解: 以3个大陆为起点,都dfs一遍,求出该大陆到其他点的最小距离是多少, 然后枚举每个点作为3个大陆的路径交点. 代码: #include<bits/stdc++ ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
- Codeforces 305B:Continued Fractions(思维+gcd)
http://codeforces.com/problemset/problem/305/B 题意:就是判断 p / q 等不等于那条式子算出来的值. 思路:一开始看到 1e18 的数据想了好久还是不 ...
- codeforces 876 F. High Cry(思维)
题目链接:http://codeforces.com/contest/876/problem/F 题解:一道简单的思维题,知道最多一共有n*(n+1)/2种组合,不用直接找答案直接用总的组合数减去不符 ...
随机推荐
- 挂起(suspend)与线程阻塞工具类LockSupport
挂起(suspend)与线程阻塞工具类LockSupport 一般来说是不推荐使用suspend去挂起线程的,因为suspend在导致线程暂停的同时,并不会去释放任何锁资源. 如果其他任何线程想要访问 ...
- DesignPattern系列__05开闭原则
介绍 开闭原则是编程设计中最基本.最重要的原则. 定义:一个软件实体如类.方法和模块等,应该对扩展(提供方)开放,对修改(使用方)关闭.用抽象构建框架,用实现扩展细节. 也就是说,在需求发生新的变化时 ...
- Linux基础文件类型
一.文件时间 ls -l 文件名 仅看的是文件的修改时间 [root@linux ~]# ls -l /etc/passwd -rw-r--r-- root root 5月 : /etc/passwd ...
- Android 9.0 关机流程分析
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- 转载:MyBatis mapper.xml中使用静态常量或者静态方法
转自:https://my.oschina.net/wtslh/blog/682704 今天偶然之间刷到了这样一篇博客,有点意外 mybatis 还可以这样使用ONGL常量的方式,该方式针对 xml的 ...
- 限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理
Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度来帮助用户保护服务的稳定性. 大家可能会问:Se ...
- Linux下,为应用程序添加桌面图标(ubuntu18.4)
一.桌面图标位置 Lniux下桌面图标储存路径为:/usr/share/applications 二.桌面图标格式 所有桌面图标格式均为desktop,即名为XXX.desktop 三.编辑内容(常用 ...
- npm 一些有用的提示和技巧
生成 package.json 我们通常执行 npm init,然后开始添加 npm 请求的信息. 但是,如果我们不关心所有这些信息,并且希望保留默认值,那么对于 npm 请求的每一条数据,我们都按 ...
- Why do I write a blog
I believe the most beautiful and elegant answer to this question is from Churchill. "On a peace ...
- 简洁实用Socket框架DotNettySocket
目录 简介 产生背景 使用方式 TcpSocket WebSocket UdpSocket 结尾 简介 DotNettySocket是一个.NET跨平台Socket框架(支持.NET4.5+及.NET ...