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 ...
随机推荐
- 复旦大学2013--2014学年第一学期(13级)高等代数I期末考试第七大题解答
七.(本题10分)设 \(A\) 为数域 \(K\) 上的 \(n\) 阶非异阵, 证明: 对任意的对角阵 \(B\in M_n(K)\), \(A^{-1}BA\) 均为对角阵的充分必要条件是 \ ...
- POJ 3320 Jessica's Reading Problem 尺取法
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- crontab执行shell脚本
*/5 * * * * cd /data/**/ && ./*.sh * * * * * /bin/sh /home/*.sh
- Spring 框架 详解 (四)------IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...
- 表头表侧边固定,方法二,丫的,复制td
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Why And When To Use Pre-Update and Pre-Insert Triggers In Oracle Forms
Whenever we commit after entering data in Oracle Forms, many triggers fires during this event and al ...
- JSONObject.fromObject
JSONObject.fromObjectjava.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRun ...
- python 列表函数
list函数: 功能:将字符创转化为列表,例: 列表基本函数: 1.元素赋值,例: 注意:通过list[0]= 'hel',如果原来位置上有值,会覆盖掉原来的. 2.分片操作 1)显示序列,例: 注意 ...
- 迪米特法则(LoD),即最少知识原则
解释: 如果两个类不必彼此直接通信,那么这两个类就不应当发生直接的相互作用.如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用. 重点: 在类的结构上,每个类都应当尽量降低成员 ...
- wordpress中文标签无法访问的解决方法
wordpress中文标签无法访问的解决方法 爱好 2年前 (2014-05-29) 7,601 8 当博客从华夏名网转移到阿里云之后,发现了不少问题,其中一个就是wordpress中文标签无 ...