题目大意:有一个M*N的矩阵,在这个矩阵里面有三个王国,编号分别是123,想知道这三个王国连接起来最少需要再修多少路。

分析:首先求出来每个王国到所有能够到达点至少需要修建多少路,然后枚举所有点求出来最少的即可。

代码如下:

---------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std; const int MAXN = ;
const int oo = 1e9+; int dir[][] = { {,},{,},{-,},{,-} };
char G[MAXN][MAXN];
struct node
{
int step[];
}a[MAXN][MAXN];
struct point
{
int x, y;
}; void BFS(int M, int N, int k)
{///第k个王国到达所有点的最短距离
queue<point> Q;
point q, s; for(int i=; i<M; i++)
for(int j=; j<N; j++)
{
if(G[i][j] == k+'')
{
q.x = i, q.y = j;
a[i][j].step[k] = ;
Q.push(q);
}
} while(Q.size())
{
q = Q.front();Q.pop(); for(int i=; i<; i++)
{
s = q;
s.x += dir[i][];
s.y += dir[i][]; if(s.x>=&&s.x<M && s.y>=&&s.y<N && G[s.x][s.y] != '#')
{
int t = (G[s.x][s.y]=='.' ? :); if(a[s.x][s.y].step[k]==- || a[q.x][q.y].step[k]+t < a[s.x][s.y].step[k])
{///因为王国之间没有不用修路,所有可能会回搜
a[s.x][s.y].step[k] = a[q.x][q.y].step[k] + t;
Q.push(s);
}
}
}
}
} int main()
{
int M, N; scanf("%d%d", &M, &N); for(int i=; i<M; i++)
scanf("%s", G[i]); memset(a, -, sizeof(a)); for(int i=; i<; i++)
BFS(M, N, i); int ans = oo; for(int i=; i<M; i++)
for(int j=; j<N; j++)
{
if(a[i][j].step[]!=- && a[i][j].step[]!=- && a[i][j].step[]!=-)
{///如果这个点三个王国都能够到达
int t = (G[i][j]=='.' ? :);
ans = min(ans, a[i][j].step[]+a[i][j].step[]+a[i][j].step[]-t*);
}
}
if(ans == oo)
ans = -;
printf("%d\n", ans); return ;
}

E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)的更多相关文章

  1. 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 ...

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

    题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...

  3. 暴搜 - Codeforces Round #327 (Div. 2) E. Three States

    E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #327 (Div. 2) B. Rebranding 水题

    B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...

  9. Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing

    http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...

随机推荐

  1. 将数据库二进制图片导出显示到EPPlus Excel2007中

    1.EPPlus Excel 控件可以参考我的另一篇博客:http://blog.163.com/pei_huiping/blog/static/206573067201281810549984/ 这 ...

  2. [DevExpress][TreeList]节点互斥

    关键代码: /// <summary> /// 节点互斥同步 /// 说明 /// eg: ///TreeListNode _node = e.Node; ///_node.SyncMut ...

  3. JavaScript—W3school

    一.JavaScript基础 1.写入HTML输出 2.对事件作出反应 3.改变HTML内容 4.改变HTML图像 5.改变HTML样式 6.验证输入 <script> Function ...

  4. DIV+CSS 网页布局之:两列布局

    1.宽度自适应两列布局 两列布局可以使用浮动来完成,左列设置左浮动,右列设置右浮动,这样就省的再设置外边距了. 当元素使用了浮动之后,会对周围的元素造成影响,那么就需要清除浮动,通常使用两种方法.可以 ...

  5. js异步脚本

    1.延迟脚本 HTML4.01为<script>标签定义了defer属性,为了表明脚本在执行时不会影响页面的构造.也就是说,脚本会在整个页面都解析完毕后再运行.因此在<script& ...

  6. Spring Cloud Eureka Server例子程序

    Spring-Cloud-Eureka-Server 及Client 例子程序 参考源代码:https://github.com/spring-cloud-samples/eureka 可以启动成功, ...

  7. Couldn't get lock for %t/vertx.log

    今天在启动vertx框架的项目时,报“Couldn't get lock for %t/vertx.log”的错误. 解决方案: 1,找出vertx.log的目录.一般在(C:\Users\Admin ...

  8. CentOS下Redis 2.2.14安装配置详解(转载)

    一. 下载redis最新版本2.2.14 cd /usr/local/src wget –c http://redis.googlecode.com/files/redis-2.2.14.tar.gz ...

  9. Flink 另外一个分布式流式和批量数据处理的开源平台

    Apache Flink是一个分布式流式和批量数据处理的开源平台. Flink的核心是一个流式数据流动引擎,它为数据流上面的分布式计算提供数据分发.通讯.容错.Flink包括几个使用 Flink引擎创 ...

  10. C# 两时间,时间间隔

    #region 返回时间差        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)        {  ...