Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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 N\times M N×M two-dimensional array which left-top corner is ( 0 , 0 ) (0,0) (0,0) and right-bottom corner is ( N − 1 , M − 1 ) (N-1,M-1) (N−1,M−1). Ignatius enters at ( 0 , 0 ) (0,0) (0,0), and the door to feng5166’s room is at ( N − 1 , M − 1 ) (N-1,M-1) (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 ) (x,y) (x,y), after a step, Ignatius can only stand on ( x − 1 , y ) , ( x + 1 , y ) , ( x , y − 1 ) (x-1,y), (x+1,y), (x,y-1) (x−1,y),(x+1,y),(x,y−1) or ( x , y + 1 ) (x,y+1) (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.
The input contains several test cases. Each test case starts with a line contains two numbers N N N and M ( 2 ⩽ N ⩽ 100 , 2 ⩽ M ⩽ 100 ) M(2\leqslant N\leqslant100,2\leqslant M\leqslant100) M(2⩽N⩽100,2⩽M⩽100) which indicate the size of the labyrinth. Then a N × M N\times M 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.
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 n 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.
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.
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
#include
using namespace std;
const int maxn=100+3;
struct step{
int x1,y1,t,x2,y2;
step(int x1=0,int y1=0,int t=0,int x2=-1,int y2=-1):x1(x1),y1(y1),t(t),x2(x2),y2(y2){}
bool operator<(const step &oth)const{return t>oth.t;}
}node[maxn][maxn];
priority_queue<step> pq;
stack<step> path;
char maze[maxn][maxn];
bool vis[maxn][maxn];
int dx,dy,n,m,res;
int mov[][2]={-1,0,1,0,0,1,0,-1};
bool chk(int x,int y){
return x<0||x>n-1||y<0||y>m-1||maze[x][y]=='X'||vis[x][y];
}
void init(){
memset(vis,false,sizeof(vis));
while(!pq.empty()) pq.pop();
pq.push(node[0][0]);
while(!path.empty()) path.pop();
dx=n-1,dy=m-1,vis[0][0]=true;
}
bool bfs(){
while(!pq.empty()){
step now=pq.top();
pq.pop();
if(now.x1==dx&&now.y1==dy)
return res=now.t,true;
for(int i=0;i<4;i++){
int nx=now.x1+mov[i][0],
ny=now.y1+mov[i][1],
nt=now.t+1;
if (chk(nx,ny)) continue;
vis[nx][ny]=true;
if(maze[nx][ny]!='.')
nt+=maze[nx][ny]-'0';
pq.push(node[nx][ny]=step(nx,ny,nt,now.x1,now.y1));
}
}
return false;
}
void printpath(){
step now=node[dx][dy];
while(now.x2!=-1&&now.y2!=-1){
path.push(now);
now=node[now.x2][now.y2];
}
now=node[0][0];
int time=1;
while(!path.empty()){
step next=path.top();
path.pop();
printf("%ds:(%d,%d)->(%d,%d)\n",time++,now.x1,now.y1,next.x1,next.y1);
if(maze[next.x1][next.y1]!='.'){
int monster=maze[next.x1][next.y1]-'0';
while(monster--)
printf("%ds:FIGHT AT (%d,%d)\n",time++,next.x1,next.y1);
}
now=next;
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i=0;i<n;i++) scanf("%s",maze[i]);
if(!bfs())
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",res);
printpath();
}
printf("FINISH\n");
}
return 0;
}
比普通的走迷宫问题多了一个路径输出,因此需要将结构体改写成路径形式,即用结构体保存 ( x 1 , y 1 ) (x_1,y_1) (x1,y1)和 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)。由于要求时间最短,再将普通的宽搜队列改成优先队列即可。此外,还多了一个在某一点上的停留时间,这个很容易修改,详见代码。