HDU1026 优先队列与路径
来源:互联网 发布:书单 知乎 编辑:程序博客网 时间:2024/05/18 00:10
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.
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.
#include <cstdio>#include <queue>#include <cstring>using namespace std;int n, m;int cost[110][110];struct Map{char val;int prex, prey;}map[110][110];const int dx[] = {0, 0, 1, -1},dy[] = {1, -1, 0, 0};struct node{int x, y, step;bool operator < (const node b) const{return step > b.step;}};priority_queue <node> q;bool input(){if (scanf("%d %d\n", &n, &m) == 2){for (int i = 0; i < n; i++){for (int j = 0; j < m; j++)scanf("%c", &map[i][j].val);getchar();}return 1;}return 0;}void bfs(){int ans = 0;node start;while (!q.empty())q.pop();memset(cost, 0, sizeof(cost));start.x = n - 1;start.y = m - 1;if (map[n - 1][m - 1].val >= '0' && map[n - 1][m - 1].val <= '9')start.step = cost[n - 1][m - 1] = map[n - 1][m - 1].val - '0';elsestart.step = 0;q.push(start);while (!q.empty()){node u = q.top();q.pop();if (u.x == 0 && u.y == 0){ans = u.step;break;}node next;for (int i = 0; i < 4; i++){next.x = u.x + dx[i];next.y = u.y + dy[i];if (next.x < 0 || next.x >= n || next.y < 0 || next.y >= m || map[next.x][next.y].val == 'X')continue;if (map[next.x][next.y].val == '.')next.step = u.step + 1;else{cost[next.x][next.y] = map[next.x][next.y].val - '0';next.step = u.step + 1 + cost[next.x][next.y];}map[next.x][next.y].prex = u.x;map[next.x][next.y].prey = u.y;map[next.x][next.y].val = 'X';q.push(next);}}int nowx = 0, nowy = 0, newx, newy;if (ans){printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);for (int i = 1; i <= ans; i++){newx = map[nowx][nowy].prex;newy = map[nowx][nowy].prey;printf("%ds:(%d,%d)->(%d,%d)\n", i, nowx, nowy, newx, newy);for (int j = 0; j < cost[newx][newy]; j++)printf("%ds:FIGHT AT (%d,%d)\n", ++i, newx, newy);nowx = newx;nowy = newy;}}elseprintf("God please help our poor hero.\n");printf("FINISH\n");}int main(){while (input())bfs();return 0;}
0 0
- HDU1026 优先队列与路径
- HDU1026 BFS&&优先队列&&路径保存
- HDU1026 bfs+优先队列
- hdu1026【bfs优先队列】
- hdu1026 优先队列+广搜
- HDU1026 Ignatius and the Princess I 优先队列宽搜+路径输出
- HDU1026 Ignatius and the Princess I(优先队列加记录路径)
- hdu1026 Ignatius and the Princess I(bfs+优先队列+记录路径)
- 广搜+优先队列+递归+hdu1026
- hdu1026深搜+广搜(优先队列)
- HDU1026 Ignatius and the Princess I(广搜与优先队列)
- hdu1026 Ignatius and the Princess I (优先队列 BFS)
- HDU1026 Ignatius and the Princess I 优先队列
- HDU1026:Ignatius and the Princess I(BFS+优先队列)
- hdu1026 Ignatius and the Princess I(BFS+优先队列)
- HDU1026 Ignatius and the Princess I(优先队列+BFS)
- 队列与优先队列
- hdu1026(bfs+优先级队列)
- c# -- 对象销毁和垃圾回收
- java中子类继承父类时是否继承构造函数呢?
- HTML5基础之代码入门
- 如何在百度地图上画镂空圆 Android
- Spring整合Hibernate出现BeanCreationException,ClassNotFoundException
- HDU1026 优先队列与路径
- wap网站访客手机抓取软件除了手机号码抓取还可以获取到哪些信息
- 深度学习框架Caffe学习笔记(8)-BP神经网络算法
- 推荐一些用CRF做图像语义分割的资源 此博文包含图片
- android中自己写一个Fragment的应用,类似QQ的一个框架
- CentOS7 执行yum 命令出错
- 矩阵快速幂
- oracle设置环境变量
- C#中显式销毁对象