Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9533    Accepted Submission(s): 2829
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找到路径(结果发现时间上又出了问题),再根据步骤数组用DFS找到相应的路径并输出,结果超了内存,改了半天终于不内存超了,结果超时。在参考了网上无数代码后,终于A了。
     bfs是肯定要的,只是,由于中途要打怪,要耗时,所有的入队的节点就不再是平行节点了,不能直接出入队,要用优先队列,对于耗时长的节点,要作适当的滞后处理(入队后,往后放),后面用一个路径结构体数组(node road[100][100])记录该节点的前一个节点的地图位置。当bfs完成后,可根据路径结构体数组内容回溯寻找路径,入栈存储找到起点后,逐个出栈输出即可,至于打怪的情况吗,根据地图数组内容检验循环即可。


这个题目,注意两点,想A就基本没问题了。
一、注意平行点。在地图中搜索时,可能要打怪,这样,该点的耗时就比其他同时搜到的点的耗时要多,也就是说,不一定所有的搜索点都是平行点,要用优先队列对打怪耗时的入队节点进行滞后处理,这样,最小耗时就不会错了,而且也不用BFS中把所有能搜的节点都搜一次了。
二、注意节约时间。其中一点在“注意一”中已经提到了,另一点就是,不要用dfs寻找路径,dfs是很耗时的(也耗内存)。只要增加一个结构体数组,记录该点的前驱节点即可,在bfs中每当搜到一个节点,并确定能遍历,就记录该点的前驱节点,最后输出的时候,将结构体数组的数据整理到栈中然后依次输出即可。


#include #include #include #include using namespace std; int n,m; char map[100][100]; int flag[100][100]; int f; int dir[4][2]={1,0,0,1,-1,0,0,-1}; struct node {     int x, y, time;     friend bool operator < (node a, node b) {         return a.time > b.time;     }//优先队列 }; struct node1 {     int x;     int y; }; node1 road[100][100]; bool inmap(int x,int y) {     if(x>=0&&x=0&&y q;     int t,step;     first.x=0;     first.y=0;     first.time=0;     flag[0][0]=0;     q.push(first);     while(!q.empty())     {         first=q.top();         q.pop();         for(int i=0;i<4;i++)     //四个方向搜索         {             next.x=first.x+dir[i][0];             next.y=first.y+dir[i][1];             next.time=first.time+1;             if(inmap(next.x,next.y)&&!flag[next.x][next.y])       //在地图中,未搜索过             {                 if(map[next.x][next.y]>'0'&&map[next.x][next.y]<='9')      //时间变化,加上打怪时间                     next.time+=map[next.x][next.y]-'0';                 if(map[next.x][next.y]!='X')                 {                     q.push(next);                     flag[next.x][next.y]=1;                     road[next.x][next.y].x=first.x;     //记录前驱节点                     road[next.x][next.y].y=first.y;                 }                 if(next.x==n-1&&next.y==m-1)    //到达终点,返回时间                     return next.time;             }         }     }     return -1;    //无法走到终点 } int main() {     while(scanf("%d%d",&n,&m)!=EOF)     {         f=0;         memset(flag,0,sizeof(flag));         for(int i=0;i s;       //根据记录步骤的数组,找出路径             node now;             now.x=n-1;     //从终点开始,找起点,求出路径             now.y=m-1;             int x,y;             while(now.x!=0||now.y!=0)             {                 s.push(now);       //找到路径,当前节点入栈                 x=road[now.x][now.y].x;                 y=road[now.x][now.y].y;                 now.x=x;                 now.y=y;             }             int t=1;    //路径已找到,节点出栈输出             printf("%ds:(0,0)",t++);             while(!s.empty())             {                 now=s.top();                 s.pop();                 printf("->(%d,%d)\n",now.x,now.y);                 if(map[now.x][now.y]>'0'&&map[now.x][now.y]<='9')     //处理打怪时间                 {                     int i=map[now.x][now.y]-'0',j;                     for(j=0;j