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种组合,不用直接找答案直接用总的组合数减去不符 ...
随机推荐
- 自定义SWT控件六之自定义Tab
6.自定义tab 本章节提供的自定义tab 分为两类 tab上带删除按钮和添加按钮,可删除tab和添加tab tab不可删除和添加 6.1 不可删除tab package com.view.contr ...
- java课堂 动手动脑2
1.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数 Modulus=231-1=int.MaxValue, Multiplier=75=16807. C=0 当显示过231-2个数 ...
- maven 打包并导出 lib 第三方jar
一. maven 导出lib 包 执行命令 mvn dependency:copy-dependencies -DoutputDirectory=target/lib 或者在 eclipse 中执行, ...
- Java 通过反射改变私有变量的值
直接上代码 import java.lang.reflect.Field; public class Main { public static void main(String[] args ...
- 伪分布式Spark + Hive on Spark搭建
Spark大数据平台有使用一段时间了,但大部分都是用于实验而搭建起来用的,搭建过Spark完全分布式,也搭建过用于测试的伪分布式.现在是写一遍随笔,记录一下曾经搭建过的环境,免得以后自己忘记了.也给和 ...
- win10 我的电脑下面的六个文件夹的隐藏
第一步 第二步 第三步 修改注册表,要隐藏那个文件夹,ThisPCPolicy 改为 "Hide" 修改我的文档的注册表值,使我的文档文件夹隐藏 <w ...
- css实现左边高度自适应右边高度
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- C#将图片转换成字符画
先看一下效果图 在Main方法中调用(首先要添加程序集System.Drawing,然后引入命名空间System.Drawing) ConvertToChar(new Bitmap(@"D: ...
- 基于jmeter+perfmon的稳定性测试记录
1. 引子 最近承接了项目中一些性能测试的任务,因此决定记录一下,将测试的过程和一些心得收录下来. 说起来性能测试算是软件测试行业内,有些特殊的部分.这部分的测试活动,与传统的测试任务差别是比较大的, ...
- 【KakaJSON手册】05_JSON转Model_05_动态模型
在上一篇文章中提到:有时候服务器返回的某个字段的内容类型可能是不确定的 当时给出的解决方案是实现kk_modelValue或者kk_didConvertToModel方法,根据实际需求自定义JSON的 ...