HDU2425:Hiking Trip(BFS+优先队列)
给出一个地图,地图有四种路面,经过每种路面花费的时间不同,问从起点到终点所花费的最少时间是多少
把到各个点的花费存入队列中,然后弹出,即可得到最小
1 2 10
T...TT
TTT###
TT.@#T
..###@
0 1 3 0
4 6
1 2 2
T...TT
TTT###
TT.@#T
..###@
0 1 3 0
2 2
5 1 3
T@
@.
0 0 1 1
Case 2: 8
Case 3: -1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int x,y,v1,v2,v3,b,n,m;
int x1,x2,y1,y2;
int p[25][25];
int vis[25][25];
int v[5];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step > b.step;
}
}; bool judge(int x,int y)
{
if(x < 0 || y < 0 || x >= n || y >= m || p[x][y] < 0 || vis[x][y])
return false;
return true;
} int work()
{
priority_queue<node>que;
node a,b;
a.x=x1;
a.y=y1;
a.step = 0;
que.push(a);
memset(vis,0,sizeof(vis));
vis[x1][y1] = 1;
while(!que.empty())
{
a = que.top();
que.pop();
if(a.x == x2 && a.y == y2)
return a.step;
for(int i = 0;i < 4;i++)
{
b = a;
b.x += dir[i][0];
b.y += dir[i][1];
if(!judge(b.x,b.y))
continue;
b.step += v[p[b.x][b.y]];
vis[b.x][b.y] = 1;
que.push(b);
}
}
return -1;
} int main()
{
int i,j,cas = 1;
char s[1000];
while(~scanf("%d%d",&n,&m))
{
scanf("%d%d%d",&v[3],&v[2],&v[1]);
for(i = 0; i < n; i++)
{
scanf("%s",s);
for(j = 0;s[j]; j++)
{
if(s[j]=='T') p[i][j] = 1;
else if(s[j] == '.') p[i][j] = 2;
else if(s[j] == '#') p[i][j] = 3;
else if(s[j] == '@') p[i][j] = -1;
}
}
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans = work();
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}
HDU2425:Hiking Trip(BFS+优先队列)的更多相关文章
- hdu 2425 Hiking Trip (bfs+优先队列)
Problem Description Hiking in the mountains is seldom an easy task for most people, as it is extreme ...
- hdu 2425 Hiking Trip
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- 201621123062《java程序设计》第六周作业总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...
- Git学习使用
1.注册码云并建立远程仓库 2.安装git 3.使用eclipse egit 推送以及克隆 建立本地仓库,成功后如图 推送项目至本地仓库与远程仓库 使用右键菜单team-share 选项,与仓库关联后 ...
- 201621123043 《Java程序设计》第1周学习总结
1. 本章学习总结 Jdk的安装: eclipse的基本使用方法 Java发展史 jdk.jre.jvm 关键词之间的联系:是整个java的核心,包括了一堆java.java基础的类库.java运行环 ...
- 在360、UC等浏览器,img不加载原因
问题:图片在360浏览器不被加载,在UC浏览器强制不显示. 前言不多说,直接上图. 360浏览器显示情况: UC浏览器显示情况: 由以上两张截图可以看到,在360浏览器,banner图片处根本没有加载 ...
- 新概念英语(1-107)It's Too Small.
Lesson 107 It's too small. 太小了. Listen to the tape then answer this question. What kind of dress doe ...
- 新概念英语(1-103)The French Test
Lesson 103 The French test 法语考试 Listen to the tape then answer this question. How long did the exam ...
- JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson
java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...
- java的分数类
概述 分数类在算法中非常重要, 而在java中不那么重要,java基础类库提供 了biginteger了,提供类似方式, package 组合数学; public class Fraction { p ...
- Python学习之参数
参数 # coding=utf-8 # 函数的参数 def power(x): return x * x; print power(5) 修改后 def power_1(x,n=2): #默认参数可以 ...
- jenkins创建multibranch pipeline
参考以下文章进行实践: https://jenkins.io/doc/pipeline/tour/hello-world/#what-is-a-jenkins-pipeline (看见一个介绍的还不错 ...