hdu1026 (bfs+ priority queue + print path)

Source: Internet
Author: User

Ignatius and the Princess I

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14577 Accepted Submission (s): 4613
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

Authorignatius.l

Recommendwe has carefully selected several similar problems for you:1072 1175 1010 1180 1016 In fact, this problem can be searched backwards, no stack.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<queue>using namespacestd;#defineMAXN 210#defineINF 0x3f3f3f3f3f3fCharMAP[MAXN][MAXN];intVISIT[MAXN][MAXN];intdir[4][2]={0,-1,0,1,-1,0,1,0};intn,m;intPRE_X[MAXN][MAXN],PRE_Y[MAXN][MAXN];intREAR_X[MAXN][MAXN],REAR_Y[MAXN][MAXN];intCNT;structnode{intx,y,dist=0;};structcmp{BOOL operator() (node A,node b) {if(a.dist>b.dist)//sort from small to large            return true; Else            return false; }};p Riority_queue<node,vector <node>,cmp>PQ;voidinit () {CNT=1; memset (Visit,0,sizeof(visit)); memset (pre_x,-1,sizeof(pre_x)); memset (pre_y,-1,sizeof(pre_y)); memset (rear_x,-1,sizeof(rear_x)); memset (Rear_y,-1,sizeof(rear_y)); pre_x[0][0]=0; pre_y[0][0]=0; Rear_x[n-1][m-1]=n-1; Rear_y[n-1][m-1]=m-1; //for (int i=1;i<maxn;i++)//printf ("%d", pre_x[i]); //printf ("%d%d\n\n", pre_x[1],pre_y[1]);      while(!pq.empty ()) Pq.pop ();}intBFS (node start) {Pq.push (start); //Value Passingnode cur; VISIT[START.X][START.Y]=1;  while(!Pq.empty ()) {cur=Pq.top ();             Pq.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.dist); intfather_x=cur.x,father_y=Cur.y,xx,yy;  while(pre_x[father_x][father_y]!=father_x | | pre_y[father_x][father_y]!=father_y) {XX=father_x; yy=father_y; Father_x=Pre_x[xx][yy]; Father_y=Pre_y[xx][yy]; Rear_x[father_x][father_y]=: x; Rear_y[father_x][father_y]=yy; }                    intson_x=0, son_y=0;  while(rear_x[son_x][son_y]!=son_x | | rear_y[son_x][son_y]!=son_y) {printf ("%ds: (%d,%d) (%d,%d) \ n", cnt++, son_x,son_y,rear_x[son_x][son_y],rear_y[son_x][son_y]); XX=son_x; yy=son_y; Son_x=Rear_x[xx][yy]; Son_y=Rear_y[xx][yy]; if(map[son_x][son_y]!='.'&& map[son_x][son_y]!='X')                         for(intI=1; I<= (map[son_x][son_y]-'0'); i++) printf ("%ds:fight at (%d,%d) \ n", cnt++, son_x,son_y); } printf ("finish\n"); return 1; }              for(intI=0;i<4; i++) {node Next; intx, y; Next.x=x=cur.x+dir[i][0]; Next.y=y=cur.y+dir[i][1]; if(0<=x && x<n &&0<=y && y<m && visit[x][y]==0&& map[x][y]!='X')                  //is only updated once, the difference between the Dijkstra optimized with the priority queue is//The weight of the update point to each of the points where it is possible to update it is fixed, so the shortest point to bounce, then update, is the shortest. //Priority Queue Optimization Dijkstra, the reason that it has been relaxed many times is that the point being bounced is the shortest path. But//The update point may have multiple paths to the update point, assuming that the update point is the last point, then the next-to-second point has a different weight from the update point.//so the shortest path to the second-to-last point on each path plus the weighted value of the next-to-penultimate point and the edge of the penultimate point may cause the update point to be updated multiple times//If, however, the weights of the points that are likely to update it are fixed in a diagram similar to hdu1026, because of the update point ,//only the shortest path to the second-to-last point can be used to update it, and the point from the priority queue is the shortest path to each point.//so we just need to change the normal queue in BFS to the priority queue.                  {                     if(map[x][y]=='.') {next.dist=cur.dist+1;                         Pq.push (next); PRE_X[NEXT.X][NEXT.Y]=cur.x; PRE_Y[NEXT.X][NEXT.Y]=Cur.y; VISIT[NEXT.X][NEXT.Y]=1; }                     Else                     {                         intstep=map[x][y]-'0'+1; Next.dist=cur.dist+step;                         Pq.push (next); PRE_X[NEXT.X][NEXT.Y]=cur.x; PRE_Y[NEXT.X][NEXT.Y]=Cur.y; VISIT[NEXT.X][NEXT.Y]=1; }                 }             }         }          return 0;}intMain () {//freopen ("Test.txt", "R", stdin);     while(~SCANF ("%d%d%*c",&n,&m)) {init ();  for(intI=0; i<n;i++) {scanf ("%s", Map[i]);            } node start; Start.x=0; Start.y=0; Start.dist=0; if(BFS (start) = =0) printf ("God Poor hero.\nfinish\n"); }    return 0;}

hdu1026 (bfs+ priority queue + print 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.