Problem Description
Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost during the trip. Recently Green has decided to go on a hiking trip. Unfortunately, half way through the trip, he gets extremely tired and so needs to find the path that will bring him to the destination with the least amount of time. Can you help him?
You've obtained the area Green's in as an R * C map. Each grid in the map can be one of the four types: tree, sand, path, and stone. All grids not containing stone are passable, and each time, when Green enters a grid of type X (where X can be tree, sand or path), he will spend time T(X). Furthermore, each time Green can only move up, down, left, or right, provided that the adjacent grid in that direction exists.
Given Green's current position and his destination, please determine the best path for him. 
 
Input
There are multiple test cases in the input file. Each test case starts with two integers R, C (2 <= R <= 20, 2 <= C <= 20), the number of rows / columns describing the area. The next line contains three integers, VP, VS, VT (1 <= VP <= 100, 1 <= VS <= 100, 1 <= VT <= 100), denoting the amount of time it requires to walk through the three types of area (path, sand, or tree). The following R lines describe the area. Each of the R lines contains exactly C characters, each character being one of the following: ‘T’, ‘.’, ‘#’, ‘@’, corresponding to grids of type tree, sand, path and stone. The final line contains four integers, SR, SC, TR, TC, (0 <= SR < R, 0 <= SC < C, 0 <= TR < R, 0 <= TC < C), representing your current position and your destination. It is guaranteed that Green's current position is reachable – that is to say, it won't be a '@' square.
There is a blank line after each test case. Input ends with End-of-File.
 
Output
For each test case, output one integer on one separate line, representing the minimum amount of time needed to complete the trip. If there is no way for Green to reach the destination, output -1 instead.
 
Sample Input
4 6
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

 
Sample Output
Case 1: 14
Case 2: 8
Case 3: -1
 
//挺简单的,之前忘记做标记了,结果MLE了
 
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n,m;
int vp,vs,vt;
int k1,k2,e1,e2;
char data[][];
int visit[][];
int to[][]={{,},{-,},{,},{,-}}; struct node
{
int x,y;
int step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
}; int go(int i,int j)
{
if(<=i&&i<n&&<=j&&j<m&&data[i][j]!='@'&&visit[i][j]==)
return ;
else return ;
} int bfs()
{
node st,ed;
priority_queue<node> q;
st.x=k1;
st.y=k2;
st.step=;
q.push(st);
memset(visit,,sizeof(visit));
visit[k1][k2]=;
while(!q.empty())
{
st=q.top();
q.pop();
if(st.x==e1&&st.y==e2)
{
cout<<st.step<<endl;
return ;
}
for(int i=;i<;i++)
{
ed.x=st.x+to[i][];
ed.y=st.y+to[i][];
if(go(ed.x,ed.y))
{
visit[ed.x][ed.y]=;
if(data[ed.x][ed.y]=='T')
ed.step=st.step+vt;
if(data[ed.x][ed.y]=='.')
ed.step=st.step+vs;
if(data[ed.x][ed.y]=='#')
ed.step=st.step+vp;
q.push(ed);
}
}
}
cout<<"-1"<<endl;
return ;
} int main()
{
int k=;
while(cin>>n>>m)
{
k++;
cin>>vp>>vs>>vt;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
cin>>data[i][j];
cin>>k1>>k2>>e1>>e2;
cout<<"Case "<<k<<": ";
bfs();
}
return ;
}

hdu 2425 Hiking Trip (bfs+优先队列)的更多相关文章

  1. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

  2. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  3. HDU2425:Hiking Trip(BFS+优先队列)

    给出一个地图,地图有四种路面,经过每种路面花费的时间不同,问从起点到终点所花费的最少时间是多少 把到各个点的花费存入队列中,然后弹出,即可得到最小 Sample Input 4 6 1 2 10 T. ...

  4. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  5. HDU 1242 Rescue (BFS+优先队列)

    题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...

  6. 2015多校第6场 HDU 5360 Hiking 贪心,优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360 题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须 ...

  7. HDU 5360——Hiking——————【贪心+优先队列】

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  8. hdu 5040 Instrusive【BFS+优先队列】

    11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...

  9. hdu.1254.推箱子(bfs + 优先队列)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

随机推荐

  1. Form标签+Css基础

      一.Form表单标签 <form action="" method=""></form>    表单就是用来将用户的信息提交到服务器 ...

  2. magento获取一些值的方法函数

    1显示产品列表页(列表.PHTML).echo $this->getProductListHtml(); 2.得到你的Magento的页面的路径.  echo $this->getUrl( ...

  3. C++友元

    通过friend关键字,我们可以将不属于当前类的一个函数在当前类中加以声明,该函数便可以成为当前类的友元函数. 例1: #include<iostream>using namespace ...

  4. Log4J1升级Log4J2

    近期,碰到需要将项目中的Log4J1升级到Log4J2,现进行下总结.交代下技术背景:web项目,基于Java + Maven 1. 依赖 <dependency> <groupId ...

  5. docker命令和后台参数

    Docker官方为了让用户快速了解Docker,提供了一个 交互式教程 ,旨在帮助用户掌握Docker命令行的使用方法. Docker 命令行 下面对Docker的命令清单进行简单的介绍,详细内容在后 ...

  6. sqlserver 更改跟踪相关知识

    数据捕获相关文章: http://www.cnblogs.com/lyhabc/p/3383484.html http://www.cnblogs.com/chenmh/p/4408825.html ...

  7. Learning from the CakePHP source code - Part I

    最近开始痛定思痛,研究cakephp的源码. 成长的路上从来没有捷径,没有小聪明. 只有傻傻的努力,你才能听到到成长的声音. 下面这篇文章虽然过时了,但是还是可以看到作者的精神,仿佛与作者隔着时空的交 ...

  8. vs2013 中已经添加了引用,编译还是提示没有添加引用

    背景:在项目中需要引用wps中的etapi.dll.下载一个wps后,在项目中添加引用后. 同时在工程中使用using命令可以自动搜索进行添加该应用.但是,编译还是通不过.提示找不到excel.在网上 ...

  9. mongodb 性能提高之利用索引, 待续

    > 10 , 用户无法忍受 >1s , 需要加装中提示 数据库对软件整体影响是不言而喻的, 那么使用 MOngoDB时 该如何提高数据库性能 第一: 索引, 相当于记忆法的 地点桩 1. ...

  10. 从NPM到CNPM

    从NPM到CNPM   原文  http://www.cnblogs.com/hufeng/p/5166479.html 主题 npm 引用NPM网站上的一句话:npm loves you ! NPM ...