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

以前写的题了,现在想整理一下,就挂出来了。

题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1)。

‘X‘为墙,‘.‘为路,数字为怪物。墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间。

比较麻烦的是需要记录路径。还要记录是在走路还是在打怪。

因为求最短路,所以可以使用bfs。

因为进过每一个点花费时间不同,所以可以使用优先队列。

因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点。当然,记录方法不止一种。

上代码——

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <queue>
  7 using namespace std;
  8
  9 struct node
 10 {
 11     int x, y, step;
 12     bool operator < (const node& a) const
 13     {
 14         return a.step < step;
 15     }
 16 };
 17
 18 int go[4][2] = {{1, 0},{-1, 0}, {0, 1}, {0, -1}};
 19
 20 int n, m, step;
 21 bool v[110][110];
 22 char mp[110][110];
 23 int last[110][110];
 24
 25 bool bfs()
 26 {
 27     node p, q;
 28     p.x = n-1;
 29     p.y = m-1;
 30     p.step = 0;
 31     if(mp[n-1][m-1] >= ‘0‘ && mp[n-1][m-1] <= ‘9‘) p.step += mp[n-1][m-1] -‘0‘;
 32
 33     priority_queue <node> que;
 34     if(mp[p.x][p.y] != ‘X‘)
 35     que.push(p);
 36     v[p.x][p.y] = 1;
 37
 38     while(!que.empty())
 39     {
 40         p = que.top();
 41         que.pop();
 42
 43         for(int i = 0; i < 4; i++)
 44         {
 45             int x = p.x+go[i][0];
 46             int y = p.y+go[i][1];
 47
 48             if(x >= 0 && x < n && y >= 0 && y < m && !v[x][y])
 49             {
 50                 if(mp[x][y] == ‘X‘) continue;
 51
 52                 q.x = x; q.y = y; q.step = p.step+1;
 53                 if(mp[x][y] >= ‘1‘ && mp[x][y] <= ‘9‘) q.step += mp[x][y]-‘0‘;
 54                 que.push(q);
 55                 v[x][y] = 1;
 56                 last[x][y] = p.x*1000+p.y;
 57                 if(x == 0 && y == 0) {step = q.step; return 1;}
 58
 59             }
 60         }
 61     }
 62     return 0;
 63 }
 64
 65 void output()
 66 {
 67     printf("It takes %d seconds to reach the target position, let me show you the way.\n", step);
 68     int x = 0;
 69     int y = 0;
 70     int i = 1;
 71     while(x != n-1 || y != m-1)
 72     {
 73         if(mp[x][y] >= ‘1‘ && mp[x][y] <= ‘9‘)
 74         {
 75             int stop = mp[x][y] - ‘0‘;
 76             while(stop--)
 77             {
 78                 printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y);
 79             }
 80         }
 81         printf("%ds:(%d,%d)->(%d,%d)\n", i++, x, y, last[x][y]/1000, last[x][y]%1000);
 82
 83         int t = last[x][y]/1000;
 84         y = last[x][y]%1000;
 85         x = t;
 86     }
 87     if(mp[x][y] >= ‘1‘ && mp[x][y] <= ‘9‘)
 88     {
 89         int stop = mp[x][y] - ‘0‘;
 90         while(stop--)
 91         {
 92             printf("%ds:FIGHT AT (%d,%d)\n", i++, x, y);
 93         }
 94     }
 95 }
 96
 97 int main()
 98 {
 99     //freopen("test.txt", "r", stdin);
100     while(~scanf("%d%d", &n, &m))
101     {
102         memset(mp, 0, sizeof(mp));
103         memset(v, 0, sizeof(v));
104         memset(last, 0, sizeof(last));
105         for(int i = 0; i < n; i++)
106         {
107             scanf("%s", mp[i]);
108         }
109
110         if(bfs()) output();
111         else printf("God please help our poor hero.\n");
112         printf("FINISH\n");
113     }
114     return 0;
115 }

时间: 2024-10-05 04:27:25

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)的相关文章

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 (Java/Others) Total Submission(s): 14184    Accepted Submission(s): 4474 Special Judge Problem Description The Princess has been abducted by the BEelzeb

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 (Java/Others)Total Submission(s): 11700    Accepted Submission(s): 3653Special Judge Problem Description The Princess has been abducted by the BEelzebub

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 (Java/Others) Total Submission(s): 16269    Accepted Submission(s): 5164 Special Judge Problem Description The Princess has been abducted by the BEelzeb

HDU 1026 Ignatius and the Princess I (BFS)

题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <iostream> 5 6 using namespace std ; 7 8 struct node 9 { 10 int x,y ; 11 int tim ; 12 friend bool operator

HDU 1026 Ignatius and the Princess I 迷宫广搜剪枝问题

本题是个经典的迷宫广搜问题类型了.网上看到好多解法. 很多解题报告都没什么分析,更不会指出其中的关键点.代码更加像一大抄.有人分析也一大篇分析,不过全部都不切中关键,甚至在分析什么广搜和深搜区别,广搜为什么快之类的,还有喊什么暴搜之类的,全错了.估计这些代码都是抄过的. 通过一大段的时间研究,终于搞通了. 本题虽然可以说是广搜,但是其中的关键却是剪枝法,为什么呢? 因为迷宫并不能简单地广搜就能搜索出所有路径的,甚至只要迷宫大点就不能搜索出是否有路径,如果没有条件剪枝的情况下:不信,你严格写一个广

HDU 1026 Ignatius and the Princess I (基本算法-BFS)

Ignatius and the Princess I 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 si

HDU1026--Ignatius and the Princess I(BFS记录路径)

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 labyrint

hdu 1026 Ignatius and the Princess I 广搜+优先队列+记录路径

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13414    Accepted Submission(s): 4232 Special Judge Problem Description The Princess has been abducted by the BEelzeb

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 (Java/Others)Total Submission(s): 19800    Accepted Submission(s): 6452Special Judge Problem Description The Princess has been abducted by the BEelzebub