HDU 1026.Ignatius and the Princess I

Ignatius and the Princess I
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX. 5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX1 5 6 .XX... ..XX1. 2...X. ...XX. XXXXX.

Sample Output

It takes 13 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) FINISH It takes 14 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) 14s:FIGHT AT (4,5) FINISH God please help our poor hero. FINISH
 
 
这道题牵扯到了权值
因此,不能单纯地使用BFS
在牵扯权值时,我们要不断走最短的路,使用dijkstra算法
以怪物的HP+1(移动耗时+战斗耗时)为权值(没有怪物权值为1)
 
由于最后要输出路径, 因此每次进行松弛操作(更新距离)要记录下最短的路径
最后输出需要正序输出,因此可使用栈
 
  1 /*
  2 By:OhYee
  3 Github:OhYee
  4 Email:oyohyee@oyohyee.com
  5 Blog:http://www.cnblogs.com/ohyee/
  6 
  7 かしこいかわいい?
  8 エリーチカ!
  9 要写出来Хорошо的代码哦~
 10 */
 11 
 12 #include <cstdio>
 13 #include <algorithm>
 14 #include <cstring>
 15 #include <cmath>
 16 #include <string>
 17 #include <iostream>
 18 #include <vector>
 19 #include <list>
 20 #include <queue>
 21 #include <stack>
 22 #include <map>
 23 using namespace std;
 24 
 25 //DEBUG MODE
 26 #define debug 0
 27 
 28 //循环
 29 #define REP(n) for(int o=0;o<n;o++)
 30 
 31 
 32 const int maxn = 155;
 33 
 34 int N,M;
 35 char Map[maxn][maxn];
 36 pair<int,int> last[maxn][maxn];
 37 int dis[maxn][maxn];
 38 
 39 struct point {
 40     int x,y;
 41     int dis;
 42     point(int a,int b,int c) {
 43         x = a;
 44         y = b;
 45         dis = c;
 46     }
 47     bool operator < (const point &rhs)const {
 48         return dis > rhs.dis;
 49     }
 50 };
 51 
 52 
 53 const int delta[] = {1,-1,0,0};
 54 
 55 int BFS() {
 56     priority_queue<point> Q;
 57     bool visited[maxn][maxn];
 58     memset(visited,false,sizeof(visited));
 59     memset(dis,-1,sizeof(dis));
 60 
 61     Q.push(point(0,0,0));
 62     dis[0][0] = 0;
 63 
 64     while (!Q.empty()) {
 65 
 66         int th1 = Q.top().x;
 67         int th2 = Q.top().y;
 68         int thdis = Q.top().dis;
 69         Q.pop();
 70 
 71         if (visited[th1][th2] == true)
 72             continue;
 73         visited[th1][th2] = true;
 74 
 75         //达到终点
 76         //if (th1 == N - 1 && th2 == M - 1)
 77         //    return dis[N - 1][M - 1];
 78 
 79         //拓展节点
 80         REP(4) {
 81             int next1 = th1 + delta[o];
 82             int next2 = th2 + delta[3 - o];
 83 
 84             if (Map[next1][next2] != 'X' && next1 >= 0
 85                 && next1 < N && next2 >= 0 && next2 < M) {
 86 
 87                 int weight;
 88                 if (Map[next1][next2] == '.')
 89                     weight = 1;
 90                 else
 91                     weight = Map[next1][next2] - '0' + 1;
 92 
 93                 int temp = dis[next1][next2];
 94 
 95                 dis[next1][next2] = dis[next1][next2] == -1 ?
 96                     dis[th1][th2] + weight :
 97                     min(dis[th1][th2] + weight,dis[next1][next2]);
 98 
 99                 if (temp != dis[next1][next2]) {//记录路径
100                     last[next1][next2] = pair<int,int>(th1,th2);
101                     //printf("%d %d -> %d %d\n", th1, th2, next1, next2);
102                 }
103 
104                 Q.push(point(next1,next2,dis[next1][next2]));
105 
106             }
107 
108         }
109 
110     }
111     if (dis[N - 1][M - 1])
112         return dis[N - 1][M - 1];
113     else
114         return -1;
115 }
116 
117 bool Do() {
118     if (scanf("%d%d",&N,&M) == EOF)
119         return false;
120     for (int i = 0; i < N; i++)
121         for (int j = 0; j < M; j++)
122             scanf("\n%c\n",&Map[i][j]);
123     /*
124     for (int i = 0; i < N; i++) {
125     for (int j = 0; j < M; j++)
126     printf("%c", Map[i][j]);
127     printf("\n");
128     }
129     */
130 
131     if (BFS() == -1) {
132         printf("God please help our poor hero.\nFINISH\n");
133     } else {
134         printf("It takes %d seconds to reach the target position, let me show you the way.\n",dis[N - 1][M - 1] );
135 
136         stack<pair<int,int> > s;
137         int x = N - 1,y = M - 1;
138         while (!(x == 0 && y == 0)) {
139             //printf("s %d %d\n", x, y);
140             s.push(pair<int,int>(x,y));
141             pair<int,int> t = last[x][y];
142             x = t.first;
143             y = t.second;
144         }
145         //s.pop();
146         //s.push(point(0, 0));
147 
148         REP(dis[N - 1][M - 1]) {
149             printf("%ds:",o + 1);
150 
151             x = s.top().first;
152             y = s.top().second;
153             s.pop();
154             printf("(%d,%d)->(%d,%d)\n",last[x][y].first,last[x][y].second,x,y);
155 
156             if (Map[x][y] >= '0'&&Map[x][y] <= '9') {
157                 int wait = Map[x][y] - '0';
158                 for (o++; wait; wait--,o++)
159                     printf("%ds:FIGHT AT (%d,%d)\n",o + 1,x,y);
160                 o--;
161             }
162 
163 
164         }
165         printf("FINISH\n");
166 
167     }
168 
169 
170 
171     return true;
172 }
173 
174 int main() {
175     while (Do());
176     return 0;
177 }

 

 
posted @ 2016-04-19 21:05  OhYee  阅读(115)  评论(0编辑  收藏  举报