杭电1026

快来打我* 2022-06-04 05:53 429阅读 0赞

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7141 Accepted Submission(s): 2137
Special Judge

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

  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

解题思路:实质就是一个图的最短路径问题,可以采用bsf搜索,并且结合使用优先级队列。

代码如下:

  1. //杭电1026
  2. #include <iostream>
  3. #include <queue>
  4. #include <stack>
  5. #include <fstream>
  6. using namespace std;
  7. int dir[][2] = {
  8. {-1,0},{1,0},{0,-1},{0,1}};
  9. typedef struct node
  10. {
  11. int i, j;
  12. int time;
  13. friend bool operator<(node n1, node n2)//按照时间大小从小到大排列
  14. {
  15. return n1.time > n2.time;
  16. }
  17. }Node;
  18. typedef struct
  19. {
  20. int i;
  21. int j;
  22. }Point;
  23. char map[100][100];//记录整个地图
  24. bool used[100][100];//标记是否已经搜索过
  25. Point result[100][100];//记录达到某一点的前一个位置,通过回溯找到路径
  26. int n, m;//表示地图的大小
  27. int bfs()//广度搜索,返回到达终点的最小时间数值
  28. {
  29. priority_queue<Node> Q;//优先级队列
  30. Node start;
  31. start.i = 0;
  32. start.j = 0;
  33. start.time = 0;
  34. Point tempP;
  35. tempP.i = -1;
  36. tempP.j = -1;
  37. result[0][0] = tempP;
  38. used[0][0] = true;
  39. Q.push(start);
  40. while(!Q.empty())
  41. {
  42. Node tempN = Q.top();
  43. Q.pop();
  44. for (int k = 0; k < 4; ++ k)
  45. {
  46. int ni = tempN.i + dir[k][0];
  47. int nj = tempN.j + dir[k][1];
  48. if (ni >= 0 && ni < n && nj >= 0 && nj < m && !used[ni][nj] && map[ni][nj] != 'X')
  49. {
  50. Node temp;
  51. temp.i = ni;
  52. temp.j = nj;
  53. if (map[ni][nj] == '.')
  54. temp.time = tempN.time + 1;
  55. else
  56. temp.time = tempN.time + map[ni][nj] - '0' + 1;
  57. used[ni][nj] = true;
  58. Point tp;
  59. tp.i = tempN.i;
  60. tp.j = tempN.j;
  61. result[ni][nj] = tp;
  62. if (ni == n - 1 && nj == m - 1)
  63. return temp.time;
  64. Q.push(temp);
  65. }
  66. }
  67. }
  68. return -1;
  69. }
  70. int main ()
  71. {
  72. //ifstream in("in.txt");
  73. while (cin >> n >> m)
  74. {
  75. for (int i = 0; i < n ; ++ i)
  76. for (int j = 0; j < m ; ++ j)
  77. {
  78. cin>>map[i][j];
  79. used[i][j] = false;
  80. }
  81. //cout<< "输入完毕!"<<endl;
  82. int t = bfs();
  83. if (t == -1)
  84. {
  85. cout<<"God please help our poor hero."<<endl;
  86. cout <<"FINISH"<<endl;
  87. }
  88. if (t != -1)
  89. {
  90. cout <<"It takes "<<t<<" seconds to reach the target position, let me show you the way."<<endl;
  91. stack<Point> path;
  92. Point pp ;
  93. pp.i = n - 1;
  94. pp.j = m - 1;
  95. path.push(pp);
  96. while (result[pp.i][pp.j].i != -1 && result[pp.i][pp.j].j != -1)
  97. {
  98. path.push(result[pp.i][pp.j]);
  99. pp = result[pp.i][pp.j];
  100. }
  101. int time = 1;
  102. pp = path.top();
  103. path.pop();
  104. while(!path.empty())
  105. {
  106. cout << time<<"s:("<<pp.i<<","<<pp.j<<")->("<<path.top().i<<","<<path.top().j<<")"<<endl;
  107. time ++;
  108. if (map[path.top().i][path.top().j] != '.')
  109. {
  110. int tt = map[path.top().i][path.top().j] - '0';
  111. for (int mm = 0; mm < tt; ++ mm)
  112. {
  113. cout <<time<<"s:FIGHT AT ("<<path.top().i<<","<<path.top().j<<")"<<endl;
  114. time ++;
  115. }
  116. }
  117. pp = path.top();
  118. path.pop();
  119. }
  120. cout <<"FINISH"<<endl;
  121. }
  122. }
  123. //in.close();
  124. return 0;
  125. }

发表评论

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

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

相关阅读

    相关 1060

    此题是一道数学题,也是一道技巧题,也是不能直接算的,否则会超时的!!! 此题思路: 设n^n=d.xxxx\10^(k-1),其中k表示n^n的位数; d.xxxx

    相关 2075

    此题真的是简单的再不能简单了!呵呵!我一直纠结,出这样的题是什么意思呢?不懂!哎,不说那些废话了,直接 ac吧!呵呵! \include<iostream> using

    相关 2078

    说实话,此题是一道有严重bug的问题,对于xhd没晚能复习的科目数m根本就没用上!!!哎不管那么些了,反正ac了!呵呵!此题这样想xhd得复习效率是前一课程和后一课程复习效率差

    相关 2090

    此题就是一道令人无法琢磨的题!哎!!我简直就无语了!!呵呵!竟然能出这题。。。。 废话少说,直接ac!!! \\\ 此题要想输出结果,还需要注意一下! 在linux