HDU 1026 Ignatius and the Princess I (BFS search + output path)

Source: Internet
Author: User

Title Source: hdu-1026 Ignatius and the Princess I
Ignatius and the Princess I
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14677 Accepted Submission (s): 4653
Special Judge

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.
XXXXX1
5 6
. Xx...
.. XX1.
2 ... X.
... XX.
XXXXX.

Sample Output
It takes seconds to reach the target position, let me show you the The.
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)
FINISH
It takes seconds to reach the Targe T position, let me show you the The.
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)
FINISH
God Help our poor hero.
FINISH

Main topic:
Enter a character-type maze, to go from the upper left to the lower right corner, the shortest time required and the route, each step can only go up, down, left, right, not inclined to walk, the picture is '. ' Indicates that it is possible to walk, and each step takes 1 seconds, when the figure is ' number ' indicates that it takes a number of seconds to stay, so it takes time to walk here: map[][]-' 0 ' +1; if the figure is ' X ', it is not allowed to go.
Topic Analysis:
This is called the Maze Search to find the shortest problem, and the output to include the path, is a typical search question it. The problem I used is BFS search, the first need to establish a structure to store the coordinates to each location and the total time spent, using BFS of course to use the priority queue, in this queue, spend less time on the team head, also need to build a structure to store the shortest route. , and then output the path in turn when the shortest path is found.
AC Code:

#include <stdio.h>#include <string.h>#include <queue>#define MAXUsing namespace Std;charMap[MAX] [MAX];intMark[max][max];intdx[4]={1,-1,0,0};//SaidxThe next direction to gointdy[4]={0,0,1,-1};//SaidyThe next step is to go in the direction of struct node{int x,y;int  Time; friend bool Operator < (node A,node b) {returnA. Time> B. Time;//Priority queue,}};struct edge{with short output timeintpxintPy//Used to store precursor coordinates};node Edge[max]; Edge Pre[max][max];intNm; void BFs (intSxintSY) {mark[sx][sy]=1;//Mark traversed by the coordinates of node now,Next;//Defines two structures representing the current point and the next point pre[sx][sy].px=-1;//Mark End now.x=SX; Now.y=sy; Now. Time=0;if(Map[SX] [sy]!='. ') now. Time=Map[SX] [sy]-' 0 '; Priority_queue<node>Q;//Defining priority QueuesQ.Push(now); while(!Q. empty ()) {now=Q. Top ();Q.Pop();if(now.)x==0&&now.y==0)//Jocha Find the starting point, indicating the end of the search {printf("It takes %d seconds to reach the target position, let me show you the way.\n", now. Time);intkey=1, Total=now. Time; Sx=now.x, Sy=now.y; while(pre[sx][sy].px!=-1)//Keep looking for the precursor coordinates until you find the destination {intTX=PRE[SX][SY].PX;intty=pre[sx][sy].py;printf("%ds:(%d,%d)--(%d,%d) \ n", key++,sx,sy,tx,ty);if(Map[TX] [ty]!='. ') for(intI=0;i<Map[TX] [ty]-' 0 '; i++)printf("%ds: Fight at (%d,%d) \ n", key++,tx,ty);                SX=TX;            Sy=ty; }return; } for(intI=0;i<4i++)//If you have not yet done so, extend this point to all of the points in the queue that satisfy test instructions {Next.x=now.x+dx[i];Next.y=now.y+dy[i];if(Next.x<0||Next.x>=n)Continue;if(Next.y<0||Next.y>=m)Continue;if(mark[Next.x][Next.y])Continue; mark[Next.x][Next.y]=1;if(Map[Next.x][Next.y]==' X ')Continue;if(Map[Next.x][Next.y]=='. ')Next. Time=now. Time+1;Else                Next. Time=now. Time+Map[Next.x][Next.y]-' 0 '+1; pre[Next.x][Next.y].px=now.x;//Used here to store the path pre[Next.x][Next.y].py=now.y;Q.Push(Next); }    }printf("God Poor hero.\n");}intMain () { while(SCANF ("%d%d",&n,&m)!=eof) {GetChar (); memset (Mark,0, sizeof (Mark));//Initializing an array of tokens for(intI=0; i<n;i++) { for(intj=0;j<m; j + +) scanf ("%c",&MapI [j]);//Deposit Figure GetChar (); } BFS (N-1,m-1);//Looking forward from the back, the output is the path from the previous outputprintf("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 search + output 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.