Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23315 Accepted Submission(s): 7563
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.
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
又是一道搜索+输出路径。着实写了很久,也参考了网上的代码。。。
这道题,他要的是最短的时间,而按照普通bfs搜到的是最短的路径,所以自己一开始还一直在奇怪为什么路径一直不对,后来看了别人的题解才发现这一点,了解到需要使用优先队列,使时间的优先级较高,这一部分内容还是要学习啊。
感觉这个题一个较难的点就是路径的输出了,调了半天。。。
#include<iostream>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=110;
struct node{
int x;
int y;
int time;
friend bool operator < (node a,node b)//设置优先级,使时间的优先级高,所以时间短的会排在队首
{ //看的还不是很懂,需要再翻翻书
return a.time>b.time;
}
}S,Node,ans[maxn][maxn];
int track[maxn][2];
int X[4]={-1,1,0,0};
int Y[4]={0,0,-1,1};
int m,n;
char G[maxn][maxn];
bool inq[maxn][maxn];
bool test(int x,int y){ //判断该点是否可走
if(x>=0&&x<n&&y>=0&&y<m&&inq[x][y]&&G[x][y]!='X')
return true;
else
return false;
}
int bfs(){
priority_queue<node>q;
S.x=0;
S.y=0;
if(G[0][0]!='.') //起点有怪物的情况考虑
S.time=S.time+1+(G[0][0]-'0');
inq[0][0]=false;
q.push(S);
while(q.size())
{
node top=q.top();
q.pop();
if(top.x==n-1&&top.y==m-1)
{
return top.time;
}
for(int i=0;i<4;i++)
{
int newx=top.x+X[i];
int newy=top.y+Y[i];
if(test(newx,newy))
{
Node.x=newx;
Node.y=newy;
ans[newx][newy]=top; //用来存储路径
if(G[newx][newy]=='.')
{
Node.time=top.time+1;
}
else
Node.time=top.time+1+(G[newx][newy]-'0');
inq[newx][newy]=false;
q.push(Node);
}
}
}
return -1;
}
stack<node> st;
void dfs(int x,int y){ // 深搜来把路上经过的点依次存到栈中
if(x==0&&y==0)
return ;
st.push(ans[x][y]);
dfs(ans[x][y].x,ans[x][y].y);
}
int main()
{
while(cin>>n>>m)
{
memset(inq,true,sizeof(inq));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cin>>G[i][j];
}
int cnt=bfs();
if(cnt==-1)
{
cout<<"God please help our poor hero."<<endl<<"FINISH"<<endl;
continue;
}
dfs(n-1,m-1);
int num=st.size();
for(int i=1;i<=num;i++) //依次记录把路径存到数组中
{
track[i][0]=st.top().x;
track[i][1]=st.top().y;
st.pop();
}
track[num+1][0]=n-1; //终点没在栈中,存进去
track[num+1][1]=m-1;
cout<<"It takes "<<cnt<<" seconds to reach the target position, let me show you the way."<<endl;
for(int i=1,k=1;i<=cnt;i++,k++) //输出路径,i来控制时间,即第几秒
{ //k来控制路径的输出,因为时间与路径一一并非对应,一个变量不能控制
if(G[track[k][0]][track[k][1]]!='.')
{
for(int j=i;j<i+(G[track[k][0]][track[k][1]]-'0');j++)
{
cout<<j<<"s:FIGHT AT ("<<track[k][0]<<","<<track[k][1]<<")"<<endl;
}
i=i+(G[track[k][0]][track[k][1]]-'0');
if(i>=cnt) //这个大于等于有点玄学...按照一开始我的想法应该是等于,但是wa了,改成大于等于就ac了
break;
}
cout<<i<<"s:("<<track[k][0]<<","<<track[k][1]<<")->("<<track[k+1][0]<<","<<track[k+1][1]<<")"<<endl;
}
cout<<"FINISH"<<endl;
}
return 0;
}