Ignatius and the Princess I
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 16713 Accepted Submission (s): 5327
Special Judge
Problem DescriptionThe Princess have been abducted by the Beelzebub feng5166, our hero Ignatius have to rescue our pretty Pr Incess. Now he gets into feng5166 ' s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth are a n*m two-dimensional array which left-top corner is (0,0) and Righ T-bottom Corner is (n-1,m-1). Ignatius enters at (0,0), and the door to Feng5166 's are at (n-1,m-1), that's our target. There is some monsters in the castle, if Ignatius meet them, he had to kill them. Here is some rules:
1.Ignatius can only moves in four directions, 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 was 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. Assume that the start position and the target position would never be a trap, and there would never be a monster at The start position.
Inputthe input contains several test cases. Each test case starts with a line contains the 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.
Outputfor Each test case, you should output "God's help to our poor hero." If Ignatius can ' t reach the target position, Or you should output ' It takes n seconds to reach the target position and let me show you the ' the '. (n is the minimum seconds), and the hero the whole path. Output a line contains the "FINISH" after all test case. If there is more than one path, any one was OK in this problem. More details in the Sample Output.
Sample Input5 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 Outputit takes seconds to reach the target position, let me show you the Way.1s: (0,0) (1,0) 2s: (1,0) 3s: (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 To reach the target position, let me show you the Way.1s: (0,0)--(1,0) 2s: (1,0)--(+) 3s: (+)--(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, our poor hero. FINISH
#include <iostream>#include<queue>using namespacestd;Const intmaxn= the;Const intinf=0x3f3f3f3f;structnode{intY,x,hp,step,id,pre; Node () {} node (intYintXintHpintStepintIdintpre) { This->y=y; This->x=x; This->hp=HP; This->step=step; This->id=ID; This->pre=Pre; } BOOL operator< (Constnode& NO)Const { returnStep >No.step; }}path[10005];inttot;intn,m;intT[MAXN][MAXN];CharMZ[MAXN][MAXN];intdy[4]={0,1,0,-1};intdx[4]={1,0,-1,0};intstart;intTime ;voidPrintintk) {Node now=Path[k]; if(now.pre==-1) { ++Time ; cout<<time<<"s: ("<<now.y<<","<<now.x<<"),"; return ; } print (Now.pre); cout<<"("<<now.y<<","<<now.x<<")"<<Endl; for(intI=0; i<now.hp;i++) { ++Time ; cout<<time<<"S:fight at ("<<now.y<<","<<now.x<<")"<<Endl; } Time++; if(K!=start) cout<<time<<"s: ("<<now.y<<","<<now.x<<"),";}voidBFS () {tot=0; for(intI=0; i<maxn;i++) for(intj=0; j<maxn;j++) T[i][j]=INF; Priority_queue<Node>que; Node Now (0,0,0,0, tot,-1); Path[tot++]=Now ; Que.push (now); t[0][0]=0; while(!Que.empty ()) { Now=que.top (); Que.pop (); if(now.y==n-1&&now.x==m-1) {Start=now.id; cout<<"It takes"<<path[start].step<<"seconds to reach the target position, let me show you the The."<<Endl; time=0; Print (start); return ; } for(intI=0;i<4; i++) { intny=now.y+Dy[i]; intnx=now.x+Dx[i]; if(0<=ny&&ny<n&&0<=nx&&nx<m&&mz[ny][nx]!='X') { inthp=0; if(mz[ny][nx]!='.') hp=hp+mz[ny][nx]-'0'; intnstep=now.step+hp+1; if(nstep<T[ny][nx]) {T[ny][nx]=Nstep; Node Next=Node (ny,nx,hp,nstep,tot,now.id); Que.push (next); Path[tot++]=Next; } }}} cout<<"God help our poor hero."<<Endl;}intMain () { while(cin>>n>>m) { for(intI=0; i<n;i++) Cin>>Mz[i]; BFS (); cout<<"FINISH"<<Endl; } return 0;}
HDU1026 (Time-lapse maze: bfs+ priority queue)