Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

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.

 
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

 
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

 
Sample Input
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
 
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
 
  1. #include<stdio.h>
  2. #include<queue>
  3. #include<stack>
  4. #include<iostream>
  5. using namespace std;
  6. typedef struct N
  7. {
  8. int i,j,time;
  9. char c;
  10. int fron,nam;
  11. friend bool operator<(N n1,N n2)
  12. {
  13. return n2.time<n1.time;
  14. }
  15. }node;
  16. node map[105][105];
  17. stack<node> S;
  18. int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}},h,w,maxT,flog;
  19. void InStack(node Map)
  20. {
  21. node q=Map;
  22. while(q.fron!=-1)
  23. {
  24. S.push(q);
  25. q=map[q.fron/w][q.fron%w];
  26.  
  27. }
  28. S.push(q);
  29. //printf("%d\n",p);
  30. }
  31. void BFS(void)
  32. {
  33. priority_queue<node> Q;
  34. node q,p;
  35. int e,x,y,k,i;
  36. map[0][0].c='X'; map[0][0].fron=-1;
  37. q.i=0;q.j=0;q.time=0;
  38. Q.push(q);
  39. while(!Q.empty())
  40. {
  41. q=Q.top();
  42. Q.pop();
  43. k=0;
  44. for(e=0;e<4;e++)
  45. {
  46. y=q.i+dir[e][0];
  47. x=q.j+dir[e][1];
  48. if(y>=0&&y<h&&x>=0&&x<w&&map[y][x].c!='X')
  49. {
  50. map[y][x].fron=map[q.i][q.j].nam;
  51.  
  52. if(map[y][x].c=='.')
  53. map[y][x].time=q.time+1;
  54. else
  55. map[y][x].time=q.time+map[y][x].c-'0'+1;
  56. map[y][x].c='X';
  57. p.i=y; p.j=x;p.time=map[y][x].time;
  58. if(p.i==h-1&&p.j==w-1)
  59. {
  60. maxT=map[h-1][w-1].time;
  61. InStack(map[h-1][w-1]);
  62. flog=1;
  63. //printf("%d\n",maxT);
  64. return ;
  65. }
  66.  
  67. Q.push(p);
  68. }
  69. }
  70. }
  71. }
  72. int main()
  73. {
  74. int i,j,t;
  75. node q,p;
  76. while(scanf("%d%d",&h,&w)>0)
  77. {
  78. t=0;
  79. for(i=0;i<h;i++)
  80. {
  81. getchar();
  82. for(j=0;j<w;j++)
  83. {
  84. scanf("%c",&map[i][j].c);
  85. map[i][j].i=i; map[i][j].j=j;
  86. map[i][j].nam=t++;
  87. }
  88. }
  89. if(map[h-1][w-1].c=='X')
  90. {
  91. printf("God please help our poor hero.\n");
  92. printf("FINISH\n");
  93. continue ;
  94. }
  95. flog=0;
  96. BFS();
  97. if(!flog)
  98. printf("God please help our poor hero.\n");
  99. else
  100. {
  101. p=S.top();
  102. S.pop();//printf("%d\n",map[1][4].time);
  103. printf("It takes %d seconds to reach the target position, let me show you the way.\n",maxT);
  104. for(i=1;i<=maxT;i++)
  105. {
  106. if(p.time<i)
  107. {
  108. q=S.top();
  109. S.pop();
  110. }
  111. if(p.time<i)
  112. printf("%ds:(%d,%d)->(%d,%d)\n",i,p.i,p.j,q.i,q.j);
  113. else
  114. printf("%ds:FIGHT AT (%d,%d)\n",i,p.i,p.j);
  115. p=q;
  116. }
  117. }
  118. printf("FINISH\n");
  119. }
  120. }

hdu1026 Ignatius and the Princess I (优先队列 BFS)的更多相关文章

  1. HDU1026 Ignatius and the Princess I 【BFS】+【路径记录】

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜

      此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  4. hdu1026.Ignatius and the Princess I(bfs + 优先队列)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  6. hdu---------(1026)Ignatius and the Princess I(bfs+dfs)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  7. 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 ...

  8. 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 ...

  9. HDU 1026 Ignatius and the Princess I (BFS)

    题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...

随机推荐

  1. XML&DTD&XML Schema学习

    XML(eXtensible Markup Language)可扩展的标记语言.xml在web service编程中尤为重要.在网络传输中可以作为传输数据的载体.xml作为元语言,它可以用来标记数据. ...

  2. MVVM模式应用 之介绍

    M-V-VM (1)M:即Model,由现实世界抽象出来的模型: V:即View,视图,界面,该界面与用户输入设备进行交互: 但是View与Model如何进行交互呢? Binding便可以发挥作用了, ...

  3. MVVM模式应用 之在ViewModel中使用NavigationService

    在ViewModel.cs页面中是不能使用NavigationService,那该怎么实现跳转呢? 其实在ViewModel中实现页面的跳转也很简单,下面的代码: using Microsoft.Ph ...

  4. 关于安卓启动eclipse错误:找不到元素‘d:devices'的声明

    可以把C:\Documents and Settings\Administrator\.android\devices.xml这个文件删除, 再把sdk里面tools\lib下的这个文件拷贝到你删除的 ...

  5. 【python】闰年规则

    公历闰年判定遵循的规律为: 四年一闰,百年不闰,四百年再闰. 公历闰年的简单计算方法(符合以下条件之一的年份即为闰年)1.能被4整除而不能被100整除.2.能被400整除.

  6. bitVector@ java bit自我practice##Q&A:为何int 来初始化size of bitVector?long,甚至是BigInteger等策略

    /* * BitSets are packed into arrays of "words." Currently a word is * a long, which consis ...

  7. C# Json反序列化处理

    最近换工作了 从客户端转到Web端 第一个任务就是去别人的页面上抓取数据 用到的是JSON 因为他们网站json的格式有点怪 所以 就在JSON反序列化上面 花了一点时间 首先用到的工具是http:/ ...

  8. Spring MVC 统一异常处理

    Spring MVC 统一异常处理 看到 Exception 这个单词都心慌 如果有一天你发现好久没有看到Exception这个单词了,那你会不会想念她?我是不会的.她如女孩一样的令人心动又心慌,又或 ...

  9. encodeURL() vs encodeRedirectURL()

    当用URL重写方式来管理Session的时候,通过以上两个方法把session ID写到URL中.不同点是:两个方法确定是否需要包含session ID的逻辑不同.在调用HttpServletResp ...

  10. Matlab norm 用法小记

    Matlab norm 用法小记 matlab norm (a) 用法以及实例 norm(A,p)当A是向量时norm(A,p)   Returns sum(abs(A).^p)^(1/p), for ...