HDU 1026 Ignatius and the Princess I 优先队列+路径记录

时间:2021-04-03 08:18:39

Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15752 Accepted Submission(s): 4995
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

剧毒啊!一开始输出的那句英文没打Wrong了1次,记录时间的变量没有初始化Wrong!本来能一遍过的题目,硬是要错两遍。
从(0,0)走到(M-1,N-1)很简单,用广搜优先队列走一遍就可以出来了,过程和HDU 1180一样,这个还简单一点,1180那个题还需要用奇偶判断是否可以过*,过不了*还需等待什么的,这题直接搜过去就可以。
对我来说的难点就是路径的记录,一开始是一脸懵逼,完全不知道怎么记录,看了别人的代码也是看不懂,后来问同学才知道,只需要用数组来记录每个节点的前驱节点,然后从最后一个点开始找,找到第一个节点就可以,然后放入栈中又输出出来。
思路是挺简单的,但是中间犯了个小错误,让我查错查了近2个多小时,下面代码会提到,下次一定要避免这种错误。


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
char edge[105][105];
int qqx[105][105];
int qqy[105][105];
int M,N;
int iii=0;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int sta[105][105];
int mina;
struct node{
int x,y;
int step;
friend bool operator <(node a,node b)
{
return a.step>b.step;
}

};
int Judge(int x,int y,int t)
{
if(x<0||x>=M||y<0||y>=N||edge[x][y]=='X')
return 1;
else if(edge[x][y]=='.')
{
if(sta[x][y]<=(t+1))
return 1;
return 0;
}
else if(edge[x][y]>='1'&&edge[x][y]<='9')
{
if(sta[x][y]<=(t+1+edge[x][y]-'0'))
return 1;
return 0;
}
return 0;

}
bool BFS()
{
priority_queue<node>Q;
node cur,next;
cur.x=0;
cur.y=0;
cur.step=0;
sta[0][0]=0;
Q.push(cur);
while(!Q.empty())
{
cur=Q.top();
Q.pop();
if(cur.x==M-1&&cur.y==N-1)
{
mina=cur.step;
return true;
}
for(int i=0;i<4;i++)
{
next.x=cur.x+dir[i][0];
next.y=cur.y+dir[i][1];
if(Judge(next.x,next.y,cur.step)==1)
continue;
if(edge[next.x][next.y]=='.')
next.step=cur.step+1;
else if(edge[next.x][next.y]>='1'&&edge[next.x][next.y]<='9')
next.step=cur.step+1+(edge[next.x][next.y]-'0');
sta[next.x][next.y]=next.step;
qqx[next.x][next.y]=cur.x;
qqy[next.x][next.y]=cur.y;
// cout<<cur.x+1<<" "<<cur.y+1<<endl;
Q.push(next);
}
}
return false;
}
void Print()
{
stack<node>S;
node cur,next;
int m=M-1,n=N-1;
for(int i=1;i<=mina;i++)
{



int a=qqx[m][n];//这里绝对不能偷懒写成m=qqx[m][n]
int b=qqy[m][n];//n=qqx[m][n] 谨记!!!
cur.x=a;
cur.y=b;
m=a;
n=b;
// if(cur.x==M-1&&cur.y==N-1)
// continue;
// cout<<cur.x<<" "<<cur.y<<endl;
S.push(cur);
if(cur.x==0&&cur.y==0)
break;
}
while(S.size())
{
iii++;

cur=S.top();
S.pop();
if(S.size()==0)
{next.x=M-1;
next.y=N-1;
}
else
next=S.top();
printf("%ds:(%d,%d)->(%d,%d)\n",iii,cur.x,cur.y,next.x,next.y);
if(edge[next.x][next.y]>='0'&&edge[next.x][next.y]<='9')
{
for(int i=1;i<=edge[next.x][next.y]-'0';i++)
{
iii++;
printf("%ds:FIGHT AT (%d,%d)\n",iii,next.x,next.y);

}
}

}

}
int main(int argc, char *argv[])
{
while(cin>>M>>N)
{
iii=0;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
cin>>edge[i][j];

for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
sta[i][j]=999;
for(int i=0;i<105;i++)
for(int j=0;j<105;j++)
{
qqx[i][j]=99;
qqy[i][j]=99;
}
if(BFS())
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",mina);

Print();
}
else
cout<<"God please help our poor hero."<<endl;
cout<<"FINISH"<<endl;
}
return 0;
}