HDU 1062(优先队列+BFS)

梦里梦外; 2022-06-08 14:58 261阅读 0赞

问题描述:

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

  1. 5 6
  2. .XX.1.
  3. ..X.2.
  4. 2...X.
  5. ...XX.
  6. XXXXX.
  7. 5 6
  8. .XX.1.
  9. ..X.2.
  10. 2...X.
  11. ...XX.
  12. XXXXX1
  13. 5 6
  14. .XX...
  15. ..XX1.
  16. 2...X.
  17. ...XX.
  18. XXXXX.

Sample Output

  1. It takes 13 seconds to reach the target position, let me show you the way.
  2. 1s:(0,0)->(1,0)
  3. 2s:(1,0)->(1,1)
  4. 3s:(1,1)->(2,1)
  5. 4s:(2,1)->(2,2)
  6. 5s:(2,2)->(2,3)
  7. 6s:(2,3)->(1,3)
  8. 7s:(1,3)->(1,4)
  9. 8s:FIGHT AT (1,4)
  10. 9s:FIGHT AT (1,4)
  11. 10s:(1,4)->(1,5)
  12. 11s:(1,5)->(2,5)
  13. 12s:(2,5)->(3,5)
  14. 13s:(3,5)->(4,5)
  15. FINISH
  16. It takes 14 seconds to reach the target position, let me show you the way.
  17. 1s:(0,0)->(1,0)
  18. 2s:(1,0)->(1,1)
  19. 3s:(1,1)->(2,1)
  20. 4s:(2,1)->(2,2)
  21. 5s:(2,2)->(2,3)
  22. 6s:(2,3)->(1,3)
  23. 7s:(1,3)->(1,4)
  24. 8s:FIGHT AT (1,4)
  25. 9s:FIGHT AT (1,4)
  26. 10s:(1,4)->(1,5)
  27. 11s:(1,5)->(2,5)
  28. 12s:(2,5)->(3,5)
  29. 13s:(3,5)->(4,5)
  30. 14s:FIGHT AT (4,5)
  31. FINISH
  32. God please help our poor hero.
  33. FINISH

题目题意:题目给了我们一个图,然我们求出一条最短时间的路从左上角到右下角,如果有,输出路径,没有输出那段话。

题目分析:我们从起点出发进行BFS搜索,但是我们要把点保存到优先队列中,因为我们要使得时间最短,把时间短放在前面,我们还要用一个结构体数组保存路径上每一个点的前一个点的坐标用于输出路径。

代码如下:

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<queue>
  6. #include<cstdio>
  7. #include<algorithm>
  8. using namespace std;
  9. const int maxn=100+10;
  10. struct note
  11. {
  12. int x,y;
  13. int step;
  14. bool operator<(const note& a) const{//用于排序
  15. return step>a.step;
  16. }
  17. };
  18. char mmap[maxn][maxn];
  19. bool vis[maxn][maxn];
  20. struct note ans[maxn][maxn];//保存坐标
  21. int Next[4][2]={
  22. {-1,0},{1,0},{0,-1},{0,1}};
  23. int n,m,res,sec;
  24. priority_queue<struct note> que;
  25. void bfs()
  26. {
  27. while (que.size()) que.pop();
  28. struct note s;
  29. s.x=s.y=s.step=0;
  30. que.push(s);
  31. vis[0][0]=true;
  32. while (que.size()) {
  33. struct note cur,next;
  34. cur=que.top();
  35. que.pop();
  36. if (cur.x==n-1&&cur.y==m-1) {
  37. res=cur.step;
  38. return;
  39. }
  40. for (int i=0;i<4;i++) {
  41. int dx=cur.x+Next[i][0];
  42. int dy=cur.y+Next[i][1];
  43. next.x=dx,next.y=dy;
  44. if (dx<0||dx>=n||dy<0||dy>=m||vis[dx][dy]||mmap[dx][dy]=='X') continue;
  45. ans[dx][dy].x=cur.x,ans[dx][dy].y=cur.y;//记录前一个点的坐标
  46. vis[dx][dy]=true;
  47. next.step=cur.step+1;
  48. if (mmap[dx][dy]>='1'&&mmap[dx][dy]<='9')
  49. next.step+=mmap[dx][dy]-'0';
  50. que.push(next);
  51. }
  52. }
  53. }
  54. void show(int a,int b)//递归输出路径
  55. {
  56. if (ans[a][b].x!=0||ans[a][b].y!=0)
  57. show (ans[a][b].x,ans[a][b].y);
  58. printf("%ds:(%d,%d)->(%d,%d)\n",++sec,ans[a][b].x,ans[a][b].y,a,b);
  59. if (mmap[a][b]>='1'&&mmap[a][b]<='9') {
  60. int cnt=mmap[a][b]-'0';
  61. for (int i=0;i<cnt;i++) {
  62. printf("%ds:FIGHT AT (%d,%d)\n",++sec,a,b);
  63. }
  64. }
  65. }
  66. int main()
  67. {
  68. while (scanf("%d%d",&n,&m)!=EOF) {
  69. for (int i=0;i<n;i++)
  70. scanf("%s",mmap[i]);
  71. memset (vis,false,sizeof (vis));
  72. memset (ans,0,sizeof (ans));
  73. res=sec=0;
  74. bfs();
  75. if (res==0)
  76. printf("God please help our poor hero.\n");
  77. else {
  78. printf("It takes %d seconds to reach the target position, let me show you the way.\n",res);
  79. show(n-1,m-1);
  80. }
  81. printf("FINISH\n");
  82. }
  83. return 0;
  84. }

发表评论

表情:
评论列表 (有 0 条评论,261人围观)

还没有评论,来说两句吧...

相关阅读