Ignatius and the Princess I
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 16456 Accepted Submission (s): 5221
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 This topic should be a basic search bar, a long time did not do a search, in the help of the problem of successful AC. The idea of saving the path can be borrowed later. The priority queue is used. With priority queues, the queue head has always been the least time-consuming, and this operator overload should be remembered. Save this node in the flag array from which direction, the direction of each point will only be updated once, because the first time to reach the point is optimal (also can be understood as a vis after the mark), used to record the path, recursive backtracking output.
#include <iostream>#include<cstdio>#include<cstring>#include<stack>#include<queue>using namespacestd;#defineINF 999999999structnode{intx, y; intTim; FriendBOOL operator<(Node A,node b) {returnA.tim>B.tim; }};Charmap[ the][ the];intdir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};intflag[ the][ the];intvis[ the][ the];intn,m;BOOLinside (Node nn) {if(nn.x>=0&&nn.x<n&&nn.y>=0&&nn.y<m)return 1; return 0;}intans=Inf;priority_queue<Node>PQ;intBFs () {Node sta; Sta.x=0; STA.Y=0; Sta.tim=0; Pq.push (STA); VIS[STA.X][STA.Y]=1; while(!Pq.empty ()) {Node now=Pq.top (); if(now.x==n-1&&now.y==m-1) return 1; Pq.pop (); for(intI=0;i<4; i++) {Node next; Next.x=now.x+dir[i][0]; Next.y=now.y+dir[i][1]; if(!vis[next.x][next.y]&&inside (Next) &&map[next.x][next.y]!='X') {Flag[next.x][next.y]=i+1; if(map[next.x][next.y]=='.') Next.tim=now.tim+1; ElseNext.tim=now.tim+1+map[next.x][next.y]-'0'; Pq.push (next); VIS[NEXT.X][NEXT.Y]=1; } } } return 0;}voidPrintpath (intXintYintTime ) { if(flag[x][y]==0) return; intAdd=0; if(map[x][y]!='.') Add=map[x][y]-'0'; Printpath (x-dir[flag[x][y]-1][0],y-dir[flag[x][y]-1][1],time-1-add); if(map[x][y]!='.') {printf ("%ds: (%d,%d) (%d,%d) \ n", time-add,x-dir[flag[x][y]-1][0],y-dir[flag[x][y]-1][1],x,y); for(intI=1; i<=map[x][y]-'0'; i++) printf ("%ds:fight at (%d,%d) \ n", time-add+i,x,y);} Elseprintf ("%ds: (%d,%d) (%d,%d) \ n", time,x-dir[flag[x][y]-1][0],y-dir[flag[x][y]-1][1],x,y);}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {memset (Vis,0,sizeof(VIS)); memset (Flag,0,sizeof(flag)); while(!pq.empty ()) Pq.pop (); memset (Vis,0,sizeof(VIS)); for(intI=0; i<n; i++) scanf ("%s", Map[i]); intfindit=BFS (); if(Findit) {printf ("It takes%d seconds to reach the target position, let me show you the way.\n", Pq.top (). Tim); Printpath (Pq.top (). X,pq.top (). Y,pq.top (). Tim); } Elseprintf ("God Poor hero.\n"); printf ("finish\n"); } return 0;}
View Code
Hdu_1026_ignatius and the Princess I_bfs (save path)