Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14796 Accepted Submission(s): 4692
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
题意:从左上角走到右下角最短时间路径,地图上有数字的点代表主角要在该点花费相应时间打小怪兽。
题解:刚开始用之前的模板做,随便加了个路径记录,结果一直WA,看了别人的代码是从终点到起点搜。
一开始我的路径记录是这样的
xmaps[now.x][now.y] = next.x;
ymaps[now.x][now.y] = next.y;
在这组样列上错了
5 6
.XX.1.
..X.9.
2...X.
...XX.
XXXXX.
后来改成他们那样了
代码xmaps[next.x][next.y] = now.x; ymaps[next.x][next.y] = now.y;
#include <stdio.h> #include <algorithm> #include <queue> #include <string.h> using namespace std; int n, m; int sx, sy; int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; char maps[210][210]; int xmaps[210][210]; int ymaps[210][210]; int vis[210][210]; int fight[210][210]; int judge(int x, int y) { if(x<0 || x>=n || y<0 || y>=m || maps[x][y]=='X') return 0; return 1; } struct node{ int x, y, t; friend bool operator<(node a,node b){ return a.t>b.t; } }; int bfs() { sx = n-1; sy = m-1; node now, next; priority_queue<node> qu; while(!qu.empty()) qu.pop(); now.x = sx; now.y = sy; if(maps[sx][sy]>='1' && maps[sx][sy]<='9') { now.t = maps[sx][sy] - '0' ; fight[sx][sy] = maps[sx][sy] - '0'; } else now.t = 0; qu.push(now); vis[sx][sy] = 1; while(!qu.empty()) { now = qu.top(); qu.pop(); if(now.x == 0 && now.y == 0) { return now.t; } for(int i = 0;i < 4;i++) { next.x = now.x + dx[i]; next.y = now.y + dy[i]; if(judge(next.x,next .y) && !vis[next.x][next.y]) { if(maps[next.x][next.y] <='9' &&maps[next.x][next.y] >='1') { fight[next.x][next.y] = maps[next.x][next.y] - '0'; next.t = now.t + maps[next.x][next.y] - '0' + 1; } else next.t = now.t + 1; vis[next.x][next.y] = 1; xmaps[next.x][next.y] = now.x; ymaps[next.x][next.y] = now.y; qu.push(next); } } } return -1; } int main() { int i, j; while(~scanf("%d%d", &n ,&m)) { char str[210]; memset(vis,0,sizeof(vis)); memset(fight,0,sizeof(fight)); memset(xmaps,0,sizeof(xmaps)); memset(ymaps,0,sizeof(ymaps)); for(i = 0;i < n;i++) { scanf("%s", str); for(j = 0;j < m;j++) { maps[i][j] = str[j]; } } int cnt = 1; int ans = bfs(); if(ans==-1) { printf("God please help our poor hero.\n"); printf("FINISH\n"); } else { printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans); int fx,fy, x, y; x = 0; y = 0; while(cnt <= ans) { fx = xmaps[x][y]; fy = ymaps[x][y]; printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,x,y,fx,fy); for(int k = 1;k <= fight[fx][fy];k++) { printf("%ds:FIGHT AT (%d,%d)\n",cnt++,fx,fy); } x = fx; y = fy; } printf("FINISH\n"); } } }