Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11828 Accepted Submission(s): 3705
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.
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题目描述如上。这个题目个人感觉不算简单,难点就是在路径输出时,遇到敌人的“FIGHT”输出上有些麻烦。为了解决这一问题,特意设立了一个fight[x][y]数组,标记坐标(x,y)处是否有敌人存在,如果有就将敌人的血量赋值给fight[x][y]否则赋值为0.剩下的思路就稍微简单一些,以终点为第一个结点进行BFS,优先队列的构造上使用STL<pre name="code" class="cpp">#include <cstdio> #include <cstring> #include <queue> #include <iostream> #define maxn 105 using namespace std; struct node{ int x,y,time; friend int operator < (node a,node b) { return a.time>b.time; } }; struct path{ int x,y; }pre[maxn][maxn];/*记录前驱*/ char map[maxn][maxn]; int N,M; int dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0};/*directions*/ int fight[maxn][maxn];/*杀敌坐标*/ int vis[maxn][maxn];/*访问标记,已经访问过的坐标中有路径*/ int time; priority_queue<node> Q;/*优先队列*/ int check(int x,int y) { int flag = 1; if(x < 0 || x > N-1 || y < 0 || y > M-1) flag = 0; if(vis[x][y] == 1 || map[x][y] == 'X') flag = 0; return flag; } /*入队条件判断*/ int BFS() { struct node now,next; while(!Q.empty()) Q.pop(); /*每次BFS进行时情况队列*/ now.x = N-1; now.y = M-1; if(map[now.x][now.y] >= '1' && map[now.x][now.y] <= '9') { now.time = map[now.x][now.y] - '0'; fight[now.x][now.y] = map[now.x][now.y]- '0'; } else now.time=0; vis[N-1][M-1] = 1;/*初始结点已经访问*/ Q.push(now); while(!Q.empty())/*队列不为空*/ { now = Q.top(); Q.pop(); if(now.x == 0 && now.y == 0) return now.time; for(int k = 0; k < 4; k++) { next.x = now.x + dx[k]; next.y = now.y + dy[k]; if(check(next.x,next.y)) { vis[next.x][next.y] = 1; if(map[next.x][next.y] >= '1' && map[next.x][next.y] <= '9') { fight[next.x][next.y] = map[next.x][next.y] - '0'; next.time = now.time + 1 + map[next.x][next.y] - '0'; } else next.time = now.time + 1; pre[next.x][next.y].x = now.x;/*next结点的前驱是now*/ pre[next.x][next.y].y = now.y; Q.push(next); } } } return -1; } void print_path(int now_x,int now_y,int maxtime) { int time = 1; int pre_x,pre_y; while(time <= maxtime) { pre_x = pre[now_x][now_y].x; pre_y = pre[now_x][now_y].y; /*从(0,0)点起依次找它的前驱*/ printf("%ds:(%d,%d)->(%d,%d)\n",time++,now_x,now_y,pre_x,pre_y); for(int i = 1 ; i <= fight[pre_x][pre_y] ; i++) printf("%ds:FIGHT AT (%d,%d)\n",time++,pre_x,pre_y); now_x = pre_x; now_y = pre_y; } } int main() { while(cin>>N>>M) { memset(pre,0,sizeof(pre)); memset(vis,0,sizeof(vis)); memset(fight,0,sizeof(fight)); time = 0; for(int i = 0; i < N ; i++){ for(int j = 0; j < M ; j++) cin>>map[i][j]; } int maxtime = BFS(); if(maxtime == -1) printf("God please help our poor hero.\n"); else { printf("It takes %d seconds to reach the target position, let me show you the way.\n",maxtime); print_path(0,0,maxtime); } printf("FINISH\n"); } return 0; }
在查阅相关资料的时候,看到有人把题目理解成是求最短路径的问题(通路权值看作1,有敌人时权值看作血量+1)那么用一些求最短路径的算法也是可以的,我没有尝试。