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/problem/E
Description
Since roads are always expensive, the governments of the states of the newly formed alliance asked you to help them assess the costs. To do this, you have been issued a map that can be represented as a rectangle table consisting of n rows and m columns. Any cell of the map either belongs to one of three states, or is an area where it is allowed to build a road, or is an area where the construction of the road is not allowed. A cell is called passable, if it belongs to one of the states, or the road was built in this cell. From any passable cells you can move up, down, right and left, if the cell that corresponds to the movement exists and is passable.
Your task is to construct a road inside a minimum number of cells, so that it would be possible to get from any cell of any state to any cell of any other state using only passable cells.
It is guaranteed that initially it is possible to reach any cell of any state from any cell of this state, moving only along its cells. It is also guaranteed that for any state there is at least one cell that belongs to it.
Input
The first line of the input contains the dimensions of the map n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns respectively.
Each of the next n lines contain m characters, describing the rows of the map. Digits from 1 to 3 represent the accessory to the corresponding state. The character '.' corresponds to the cell where it is allowed to build a road and the character '#' means no construction is allowed in this cell.
Output
Print a single integer — the minimum number of cells you need to build a road inside in order to connect all the cells of all states. If such a goal is unachievable, print -1.
Sample Input
4 5
11..2
#..22
#.323
.#333
Sample Output
2
HINT
题意
在n*m的地图上,有三个国家,现在要让你修路,使得花费最少,就可以让每个国家的点都可以到达其他国家任意点
保证每个国家内部相连,.可以修建道路,#不能修建
题解:
直接暴力枚举地图上的每一个点,暴力出来每个国家到这个点的最小花费就好了
代码
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std; int dx[]={,-,,};
int dy[]={,,,-};
char s[][];
int dp[][][];
int n,m;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%s",s[i]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
dp[][i][j]=dp[][i][j]=dp[][i][j]=1e8;
for(int k=;k<;k++)
{
queue<pair<int,int> > Q;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(s[i][j]==''+k)
{
Q.push(make_pair(i,j));
dp[k][i][j]=;
}
}
}
while(!Q.empty())
{
pair<int,int> now = Q.front();
int x = now.first;
int y = now.second;
Q.pop();
for(int i=;i<;i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx<||xx>=n)continue;
if(yy<||yy>=m)continue;
if(s[xx][yy]=='#')continue;
if(dp[k][xx][yy]>dp[k][x][y] + (s[x][y]=='.'))
{
dp[k][xx][yy] = dp[k][x][y] + (s[x][y]=='.');
Q.push(make_pair(xx,yy));
}
}
}
}
int ans = 1e9;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
ans = min(ans,dp[][i][j]+dp[][i][j]+dp[][i][j]+(s[i][j]=='.'));
}
}
printf("%d\n",ans>=1e8?-:ans);
}
Codeforces Round #327 (Div. 2) E. Three States BFS的更多相关文章
- Codeforces Round #327 (Div. 2) E. Three States
题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...
- 暴搜 - Codeforces Round #327 (Div. 2) E. Three States
E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...
- Codeforces Round #327 (Div. 1) C. Three States
C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standard inp ...
- Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)
Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...
- E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)
题目大意:有一个M*N的矩阵,在这个矩阵里面有三个王国,编号分别是123,想知道这三个王国连接起来最少需要再修多少路. 分析:首先求出来每个王国到所有能够到达点至少需要修建多少路,然后枚举所有点求出来 ...
- Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题
A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...
- Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers 二分 物理
D. Chip 'n Dale Rescue Rangers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...
- Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- Codeforces Round #327 (Div. 2) B. Rebranding 水题
B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...
随机推荐
- Android UI——分享一个登录缓冲界面
效果如上图 所示 :就是下面的 loading 字母会按顺序一个个的 动起来 ,很好的效果 代码说明 请参考 该文:http://blog.csdn.net/xyz_lmn/article/deta ...
- 【html】页面制作规范文档
每天都在写html/css/js代码,总结的一些页面制作的规范 文件命名规范 1) 文件目录.文件名称统一用小写的英文字母.数字.下划线组合,文件名要与表现的内容相近,不到万不得已不要以拼音作为名称, ...
- 浅谈Linux容器和镜像签名
导读 从根本上说,几乎所有的主要软件,即使是开源软件,都是在基于镜像的容器技术出现之前设计的.这意味着把软件放到容器中相当于是一次平台移植.这也意味着一些程序可以很容易就迁移,而另一些就更困难. 我大 ...
- 【JSONCpp】简介及demo
一.JSON简介 JSON 一种轻量级的数据交换格式,易于阅读.编写.解析,全称为JavsScript ObjectNotation. JSON由两种基本结构组成 ① 名字/值 对的集合,可以理解 ...
- Mahout分步式程序开发 聚类Kmeans(转)
Posted: Oct 14, 2013 Tags: clusterHadoopkmeansMahoutR聚类 Comments: 13 Comments Mahout分步式程序开发 聚类Kmeans ...
- asp.net MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别
@Html.Action:需要有对应的Action,并且Action方法有返回值.(注:处理完业务逻辑同时,也需要返回所需值) @{Html.RenderAction}:需要有对应的Action,Ac ...
- 多校5 1004 HDU5784 统计锐角三角形数目
http://acm.hdu.edu.cn/showproblem.php?pid=5784 题意:n个点,找多少个锐角三角形数目 思路:极角排序+two pointers 当前选择的点集要倍增一倍, ...
- 微软Azure已开始支持hadoop--大数据云计算
微软Azure已开始支持hadoop,对于有需要弹性大数据运算的公司可能是个好消息.据悉,近期微软已提供一个预览版的Azure HDInsight(Hadoop on Azure)服务,运行在Linu ...
- CentOS 安装 Sun JDK
1. 卸载原Open JDK 查看已安装包 rpm -qa | grep java 如果已经安装有Open JDK,可能会列出类似下面的内容: tzdata-java-2015e-.el6.noarc ...
- 从Search Sort到Join
发表于<程序员>2015年4月B的一篇文章,在博客归档下.根据杂志社要求,在自己博客发表该文章亦须注明:本文为CSDN编译整理,未经允许不得转载,如需转载请联系market#csdn.ne ...