hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
11s:(1,5)->(2,5)
#include<iostream>
#include<queue>
#include<cstdio> using namespace std; struct node{
int x,y;
int time;
friend bool operator < (node a,node b){//优先队列!!
return a.time > b.time;
}
}; struct M{
char data;
int prex,prey;
int time;
}map[][]; int n,m;
int dir[][]={,,-,,,,,-}; bool isInMap(int x,int y){
return x>=&&x<n&&y>=&&y<m;
} int bfs(){
node cur,next;
priority_queue<node> q;
cur.x = ;
cur.y = ;
cur.time = ;
map[][].data = 'X';
q.push(cur);
while(!q.empty()){
cur = q.top();
q.pop();
if(cur.x == n- && cur.y == m-)
return cur.time;
for(int i=;i<;i++){
int xx = cur.x + dir[i][];
int yy = cur.y + dir[i][];
if(isInMap(xx,yy) && map[xx][yy].data == '.'){
next.x = xx;
next.y = yy;
next.time = cur.time+;
map[xx][yy].prex = cur.x;
map[xx][yy].prey = cur.y;
map[xx][yy].time = ;
map[xx][yy].data = 'X';
q.push(next);
}
else if(isInMap(xx,yy) && map[xx][yy].data!='X'){
next.x = xx;
next.y = yy;
next.time = cur.time + (map[xx][yy].data-'') + ;//记得加上通过时间
map[xx][yy].prex = cur.x;
map[xx][yy].prey = cur.y;
map[xx][yy].time = map[xx][yy].data-'';
map[xx][yy].data = 'X';
q.push(next);
}
}
}
return -;
} void printPath(int x,int y,int time){
if(time>){
if(map[x][y].time--){
printPath(x,y,time-);
printf("%ds:FIGHT AT (%d,%d)\n",time--,x,y);
}
else{
printPath(map[x][y].prex,map[x][y].prey,time-);
printf("%ds:(%d,%d)->(%d,%d)\n",time--,map[x][y].prex,map[x][y].prey,x,y);
}
}
return ;
} int main(){
while(cin>>n>>m){
for(int i=;i<n;i++){
getchar();
for(int j=;j<m;j++){
scanf("%c",&map[i][j].data);
}
}
int time = bfs();
if(time>=){
printf("It takes %d seconds to reach the target position, let me show you the way.\n",time);
printPath(n-,m-,time);
}
else{
printf("God please help our poor hero.\n");
}
printf("FINISH\n");
}
return ;
}
hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)的更多相关文章
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
- hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I(bfs)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- Springmvc整合tiles框架简单入门示例(maven)
Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...
- JavaScript运算符
JavaScript运算符 1.算数运算符 设定a = 5. 运算符 描述 例子 结果 + 加 b=a+2 b=7 - 减 b=a-2 b=3 * 乘 b=a*2 b=10 / 除 b=a/2 b=2 ...
- SD卡驱动分析(一)
Android下的SD卡驱动与标准LINUX下的SD卡驱动好像没有太大的区别,这里就以高通的ANDROID 2.3以代表,来简要分析一下LINUX下SD卡驱动的写法.由于小弟的技术有限,分析的有错的地 ...
- 【leetcode❤python】24. Swap Nodes in Pairs
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- struts2——通配符
struts2的配置文件是 struts.xml.. 在这个配置文件里面可以使用通配符..其中的好处就是,大大减少了配置文件的内容..当然,相应付出的代价是可读性.. 使用通配符的原则是 约定高于配置 ...
- 一个js(javascript)使用案例
<script type="text/javascript"> var Row; $(function () { // $("#Sel").clic ...
- iOS - Swift NSNull 空值
前言 public class NSNull : NSObject, NSCopying, NSSecureCoding 作为占据空间的一个空值,如用在数组或字典中占据一个没有任何值的空间. 1.NS ...
- parseInt 的第二个参数
["1","2","3"].map(parseInt) //[1,NaN,NaN] ["1","2" ...
- 【51nod】1376 最长递增子序列的数量
数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可能有很多个. ...
- default(T)的含义
default(T)是泛型中初始化的用法.因为对于泛型T你不知道是值类型还是引用类型,所以传参数是可能会出错.这里就要用到default(T). T t=default(T),就是初始化,值类型的话, ...