E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)
题目大意:有一个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(广搜)的更多相关文章
- 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 #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 #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 ...
- Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing
http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...
随机推荐
- easy ui tabs 顶部绑定事件
$(function(){ $('#tb').tabs('bindclick', function(index, title){ }); }); $.extend($.fn.tabs. ...
- 『奇葩问题集锦』npm install 报错 node-pre-gyp ERR! node-pre-gyp -v v0.6.25
gyp ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you ...
- js 不可变的原始值和可变的对象引用
javascript中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着根本区别.原始值是不可更改的:任何方法都无法更改(或“突变”)一个原始值.对数字和布尔值 ...
- js 验证输入框金额
$("#ipt1").keyup(function () { var reg = $(this).val().match(/\d+\.?\d{0,2}/); var txt = ' ...
- 读《CSCW的一种建模与实现方法》
这篇论文为我们描述了作者构建的一种基于交互.活动.协作三层结构的协同工作模型,并提出了一种采用“镜头焦点”和“自由交互”相结合的协作模型实现方法. 计算机支持的协同工作就是利用计算机技术将时间上分离. ...
- 为什么很多语言选择在JVM上实现
非常经济地实现跨平台.你的语言编译器后端只需要输出 JVM 字节码就可以.跨平台需要极大的工作量,举个例子,只是独立开发生成本地代码,就需要花费大量精力去针对不同平台和处理器进行优化(比如 Firef ...
- CCTV评论员评论步行者与奇才的比赛
步行者客场迎战主场作战的奇才,奇才的战士可能由于过度兴奋或是过度紧张身体僵硬,本来能打进的球都失掉了.反而,由于步行者取得了两位数的领先,越大心情越放松,打出了过去很少见的流畅局面. CCTV评论员就 ...
- ∑–△型模数转换器(ADC)简介
∑–△型模数转换器(ADC) 1.概述 近年来,随着超大规模集成电路制造水平的提高,Σ-Δ型模数转换器正以其分辨率高.线性度好.成本低等特点得到越来越广泛的应用.Σ-Δ型模数转换器方案早在20世纪60 ...
- js写分页
jsp:< input value ="1" id ="current" type ="hidden"/> <div id ...
- LightOj_1364 Expected Cards
题目链接 题意: 一副牌, 每个花色13张牌,加上大小王,共54张. 遇到大小王可以代替其中某种花色. 给定C, D, H, S. 每次抽一张牌, 问抽到C张梅花, D张方块, H张红桃, S张黑桃所 ...