HDU 1026 Ignatius and the Princess I (BFS + record Path)

Source: Internet
Author: User

Ignatius and the Princess ITime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14184 Accepted Submission (s): 4474
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 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





/* Start point (0,0) end point (n-1,m-1), ' x ' means cannot go, '. ' Can walk, walk to take a second, the number represents can walk, but walk over a second also need to add this number priority queue, for the record path to open a path array record this point is the direction from which to go, and then recursive output path */#include <iostream > #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include < queue> #include <stack> #include <vector> #include <set> #include <map> #define L (x) (x<  <1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __int64 ll; #define FRE (I,A,B) for (i = A; I <b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #defin e SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #defi Ne sfff (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define Bug pf ("hi\n") using name  Space STD; #define N 105struct stud{Stud (int xx,int yy,int s): X (xx), Y (yy), step (s) {};  Stud () {};  int x,y,step; BOOL operator< (const stud B) conSt {return step>b.step; }};int n,m;priority_queue<stud>q;int path[n][n];int vis[n][n];int step[4][2]={1,0,-1,0,0,1,0,-1};int All;char C[n][n];bool judge (int x,int y) {if (x>=0&&x<n&&y>=0&&y<m) return True;return false;} void Show (int x,int y) {if (x==0&&y==0) return; int Xx=x-step[path[x][y]][0];int Yy=y-step[path[x][y]][1];show ( XX,YY);p rintf ("%ds: (%d,%d) (%d,%d) \ n", ++all,xx,yy,x,y); if (c[x][y]>= ' 0 ' &&c[x][y]<= ' 9 ') {int i=c [x] [y]-' 0 '; while (i--) printf ("%ds:fight at (%d,%d) \ n", ++all,x,y);}} BOOL BFs () {int I,j;stud cur,next;while (!q.empty ()) Q.pop (); mem (vis,0); vis[0][0]=1;cur.x=0;cur.y=0;cur.step=0; Q.push (cur), while (!q.empty ()) {cur=q.top (); Q.pop (); if (cur.x==n-1&&cur.y==m-1) {printf ("It takes%d seconds    To reach the target position, let me show you the way.\n ", cur.step);    all=0;    Show (N-1,M-1); return true;}           for (i=0;i<4;i++) {int xx=cur.x+step[i][0];           int yy=cur.y+step[i][1];if (!judge (XX,YY)) continue;           if (Vis[xx][yy]) continue;           if (c[xx][yy]== ' X ') continue;           Next.x=xx;           Next.y=yy;           next.step=cur.step+1;           if (c[xx][yy]>= ' 0 ' &&c[xx][yy]<= ' 9 ') next.step+=c[xx][yy]-' 0 ';           Q.push (next);           vis[xx][yy]=cur.step+1;  Path[xx][yy]=i;}} return false;}   int main () {int i,j;  while (~SCANF ("%d%d", &n,&m)) {for (i=0;i<n;i++) scanf ("%s", C[i]);  if (!BFS ()) printf ("God's help to our poor hero.\n");   printf ("finish\n"); } return 0;} /**/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1026 Ignatius and the Princess I (BFS + record Path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.