Problem Description
The princess have been abducted by the Beelzebub feng5166 and our hero Ignatius have 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 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.
Input
The 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.
Output
For 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, 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 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 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, 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) 14s:fight at (4,5) Finishgod, our poor hero. FINISH
Author
Ignatius.l
Ideas:
See the picture to know is search .....
Initially want to use DFS because DFS is good to write, but because the shortest path required, DFS bad processing, so directly with BFS
The only hard part is how to record the path
Because when the first time a point is BFS, the path must be the shortest path to the starting point, so just record the precursor of this node, and the final print will be traced back by the end point.
Of course, you can also start searching from the end point, and it's easier to print the path
LRJ's algorithmic Competition Primer Classic also has a similar length introduction BFS record path
Code:
//HDU 1026//Light Search + priority queue, need to record the path#include <stdio.h>#include<queue>#include<iostream>#include<string.h>using namespacestd;Const intMAXN = the;structnode{intx, y; intTime ; FriendBOOL operator< (Node A, Node B)//Time -Small priority high { returnA.time >B.time; }};p Riority_queue<node>que;//Priority Queuestructcmap{intNX, NY;//record precursor, used to record the path CharC;} MAP[MAXN][MAXN]; //Record MapintN, M;intFIGHT[MAXN][MAXN], MARK[MAXN][MAXN];intBFS ()//start the search from the target point and search for the starting point. Record the search's precursor, and record the path.{ intK; intdir[4][2] = {{1,0}, { -1,0}, {0,1}, {0, -1}}; Node now, next; while(!que.empty ()) Que.pop ();//Initializenow.x = N-1; NOW.Y = M-1; if(Map[now.x][now.y].c >='1'&& map[now.x][now.y].c <='9') {Now.time= Map[n-1][m-1].C-'0'; FIGHT[NOW.X][NOW.Y]= MAP[NOW.X][NOW.Y].C-'0'; } ElseNow.time =0; Que.push (now); while(!Que.empty ()) { Now=Que.top (); Que.pop (); if(now.x = =0&& Now.y = =0)returnNow.time; for(k =0; K <4; k++) {Next.x= now.x + dir[k][0]; Next.y= Now.y + dir[k][1]; if(Next.x >=0&& next.x < n && next.y >=0&& Next.y < M &&!mark[next.x][next.y] && map[next.x][next.y].c! ='X') { if(Map[next.x][next.y].c >='1'&& map[next.x][next.y].c <='9') {Next.time= Now.time + MAP[NEXT.X][NEXT.Y].C-'0'+1; FIGHT[NEXT.X][NEXT.Y]= MAP[NEXT.X][NEXT.Y].C-'0'; } ElseNext.time = Now.time +1; Que.push (next); Map[next.x][next.y].nx=now.x; Map[next.x][next.y].ny=now.y; MARK[NEXT.X][NEXT.Y]=1; } } } return-1;}intMain () {intI, J, flag, X, Y, TX, ty, sec; while(SCANF ("%d%d", &n, &m)! =EOF) { for(i =0; I < n; i++) for(j =0; J < M; J + +) {scanf ("%c", &MAP[I][J].C);//The front space is criticalMARK[I][J] = fight[i][j] =0; } mark[n-1][m-1] =1; Flag=BFS (); if(Flag! =-1) {printf ("It takes%d seconds to reach the target position, let me show you the way.\n", flag); SEC=1, x = y =0; while(sec! = flag +1) {printf ("%ds: (%d,%d) (%d,%d) \ n", sec++, x, Y, Map[x][y].nx, map[x][y].ny); for(i =0; i < Fight[map[x][y].nx][map[x][y].ny]; i++) printf ("%ds:fight at (%d,%d) \ n", sec++, Map[x][y].nx, Map[x][y].ny); TX=Map[x][y].nx; Ty=Map[x][y].ny; X= TX; y =Ty; } } Elseprintf ("God Poor hero.\n"); printf ("finish\n"); } return 0;}
Hdu1026--ignatius and the Princess I (BFS record Path)