Ignatius and the Princess I(bfs+优先队列+输出路径)

来源:互联网 发布:国债期货模拟软件 编辑:程序博客网 时间:2024/06/06 09:07

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19460    Accepted Submission(s): 6304
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.XXXXX15 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)FINISHIt 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)FINISHGod please help our poor hero.FINISH
 
#include<iostream>#include<queue>#include<stack>#include<cstdio>#include<string.h>using namespace std;const int SIZE=120;struct Record {    int i;    int j;    int cost;//总耗时     bool operator<(const Record &a) const    {        return cost>a.cost;    }}temp,temp1;struct node{    int i;intj;int num;}pre[120][120];//前驱结点 int mg[SIZE][SIZE];//定义一个迷宫数组int b[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int m,n;//迷宫大小,行列值 int mins;//最短路径的长度算是求出来了,接下来关键在于如何记录路径上的点bool bfs(int x1,int y1,int x2,int y2)//bfs的算法算是写好了 {  priority_queue<Record> q;    int bx,by;  //int bx1,by1;  int flag[SIZE][SIZE];//标志数组最好放进来,这样每次调用函数时重新建立,会将里面的数据清0;      memset(flag,0,sizeof(flag));//在使用flag数组的时候,最好先清一下0,不然会WA,我不知道他的测试数据是怎样的,但是不清0答案貌似也没有错误   flag[x1][y1]=1;//走过得路径标记为1,表示不能再走   temp1.i=x1;  temp1.j=y1;  temp1.cost=0;//起点的耗时为0   q.push(temp1);//入队列   while(!q.empty())//不是吧,直接就在这里完了  { temp=q.top();//取队顶元素 q.pop();//出队列  if(temp.i==x2 && temp.j==y2)  { mins=temp.cost;//直到终点的花费就是最少花费 return true; }             // printf("出队列的元素为%d,%d\n",temp.i,temp.j); for(int i=0;i<4;i++) {    bx=temp.i+b[i][0];    by=temp.j+b[i][1];if(bx>=0 && bx<m && by>=0 && by<n && !flag[bx][by] && mg[bx][by]!=-1){ temp1.i=bx;                     temp1.j=by;     temp1.cost=temp.cost+mg[bx][by];                  pre[bx][by].i=temp.i;//bx,by是现在结点的下标值pre[bx][by].j=temp.j;//用来记录前驱结点的下标值pre[bx][by].num=mg[temp.i][temp.j];                 q.push(temp1);//入队列!    flag[bx][by]=1;//标记为1;走过了之后就不能再走了! }//if    }//for   }//while return false;           }   void print(){int k=1,i;int x1,y1;int x2,y2;    stack<node> s;node temp,temp1;    temp1.i=m-1; temp1.j=n-1; temp1.num=mg[m-1][n-1];s.push(temp1);for(x1=m-1,y1=n-1;;)//从终点开始,一级级地向前压栈{x2=x1;    y2=y1;      s.push(pre[x2][y2]);//不断的压栈x1=pre[x2][y2].i;//不断地向前搜寻前结点y1=pre[x2][y2].j;//一直压到了x1=0,y1=0;if(x1==0 && y1==0) break;}    while(!s.empty()){if(s.size()-1==0 && s.top().num==1) break;temp=s.top();s.pop();if(s.size()>0)temp1=s.top();if(temp.num==1)printf("%ds:(%d,%d)->(%d,%d)\n",k++,temp.i,temp.j,temp1.i,temp1.j);else{for(i=1;i<temp.num;i++)printf("%ds:FIGHT AT (%d,%d)\n",k++,temp.i,temp.j);if(s.size()>0)printf("%ds:(%d,%d)->(%d,%d)\n",k++,temp.i,temp.j,temp1.i,temp1.j);}}}int main(){    int i,k,x1,y1;char str[SIZE];//字符串数组while(scanf("%d %d",&m,&n)>0){ for(i=0;i<m;i++)//m行 {            cin>>str;//输入字符串for(k=0;k<n;k++)//n列{if(str[k]=='.') mg[i][k]=1;else if(str[k]=='X') mg[i][k]=-1;else mg[i][k]=str[k]-'0'+1;} }         if(bfs(0,0,m-1,n-1)) {            printf("It takes %d seconds to reach the target position, let me show you the way.\n",mins);            print(); }     else     printf("God please help our poor hero.\n");     printf("FINISH\n");}  return 0;}



 
阅读全文
0 0