Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16879 Accepted Submission(s): 5395
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
Author
Ignatius.L
题意:一个人从左上角出发,到达有下角,‘.’是路,‘X’是墙,数字代表怪物,遇到怪物要把它消灭才能通过,消灭怪兽的时间等于其数值。很明显的要用优先队列+BFS,但记录路径有点困难。
路径怎么记录呢?先举一个简单的例子,假设路径只是一个点,我们用a[]表示,其值与下标的关系:
若a[x]=y,代表到达x这个点的上一点的坐标是y。
此题要用一个结构体的数组,每次BFS的时候,如果下一步可以走,便把当前坐标(上一步)记录下来。
输出的时候倒着输出就可以了。
AC代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn=105;
char a[maxn][maxn];
bool vis[maxn][maxn];
int n,m,ans;
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
bool flag;
struct node
{
int x,y,t;
friend bool operator<(node a, node b)
{
return a.t>b.t;
}
};
node pre[maxn][maxn];
bool OK(node no)
{
if(no.x<0||no.x>=n||no.y<0||no.y>=m)
return false;
return true;
}
void BFS()
{
memset(vis,false,sizeof(vis));
vis[0][0]=true;
priority_queue<node>q;
node now,next;
now.x=0;
now.y=0;
now.t=0;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
if(now.x==n-1&&now.y==m-1)
{
ans=now.t;
flag=true;
break;
}
for(int i=0; i<4; i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(OK(next)&&!vis[next.x][next.y]&&a[next.x][next.y]!='X')
{
vis[next.x][next.y]=true;
if(a[next.x][next.y]=='.')
next.t=now.t+1;
else
next.t=now.t+(a[next.x][next.y]-'0')+1;
q.push(next);
pre[next.x][next.y].x=now.x;
pre[next.x][next.y].y=now.y;
pre[next.x][next.y].t=now.t;
}
}
}
}
void show()
{
stack<node> s;
node now;
now=pre[n-1][m-1];
now.x=n-1;
now.y=m-1;
//s.push(now);
while(now.x!=0||now.y!=0)
{
s.push(now);
now=pre[now.x][now.y];
}
int t=1;
while(!s.empty())
{
now=s.top();
s.pop();
if(a[now.x][now.y]=='.')
{
printf("%ds:(%d,%d)->(%d,%d)\n",t++,pre[now.x][now.y].x,pre[now.x][now.y].y,now.x,now.y);
}
else
{
printf("%ds:(%d,%d)->(%d,%d)\n",t++,pre[now.x][now.y].x,pre[now.x][now.y].y,now.x,now.y);
int k=a[now.x][now.y]-'0';
while(k--)
printf("%ds:FIGHT AT (%d,%d)\n",t++,now.x,now.y);
}
}
}
int main()
{
while(cin>>n>>m)
{
for(int i=0; i<n; i++)
cin>>a[i];
ans=0;
flag=false;
pre[0][0].x=pre[0][0].y=0;
BFS();
if(flag)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
show();
}
else
{
printf("God please help our poor hero.\n");
}
cout<<"FINISH"<<endl;
}
return 0;
}