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.
 

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
原创粉丝点击