HDU 1026 Ignatius and the Princess I (BFS)

Source: Internet
Author: User
Tags time 0

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

Test instructions: from (0,0) to (n-1,m-1) '. ' Represents the road, ' X ' stands for obstacles, ' n ' with the Table Monster blood volume, hit can be over, consumption time n

Topic thought:

Wide Search (BFS)

This is my first contact to the wide search, the general search for a wide range, and then combined with the previous deep search, summarized as follows:

Deep search is related to the stack:

To take a tree as an example, a deep search is one branch of the tree to find another branch until the end of the condition is met or the branch is finished.

Say the relationship with the stack: stack, advanced, that is, the root of the tree in the stack above the stack of elements can be swapped in turn, you can traverse the entire structure

Say it again in the picture: that is, from the starting point to the end of the return to a step and then go down, until the condition is satisfied or the traversal is complete.

Kwong Search is related to the team:take a tree to do an example,A wide sweep of a layer of traverse, like a tree, a layer of spreading of the leafyAgain, the relationship with the queue: The queue is FIFO, that is, we get a node can all his child nodes into the team, and always put a layer of node traversal completed before going to the next layerAgain, in the picture: from the beginning, one layer to the outside (similar to the visual effect of a droplet spreading on paper) until the condition is met or the traversal is complete.

AC Code:
#include <iostream> #include <queue> #include <stack> #include <stdlib.h> #include <iomanip >using namespace Std;int n,m;char map[101][101];typedef struct point{int x,y,step,time;//step used to record the direction of this point, step= 1,2,3,4 up and down}*prt;//because the team elements need to be modified, with the pointer prt t[100][100];bool cheak (int x,int y)//To determine whether the {if (x<0| | y<0| | x>=n| | y>=m| | map[x][y]== ' x ') return False;return true; Wide search traverse all possible void BFs () {queue<prt> q;//because the elements in the team need to be modified, with the pointer prt corrent;int x,y,k,i;bool flag;t[0][0]->time=0;// Starting point time 0 Q.push (t[0][0]);//starting point into the team while (! Q.empty ()) {Corrent=q.front ();x=corrent->x;y=corrent->y; Q.pop ();//for the upper and lower left and right four points to view://If it can be reached, and go from the current point in the past than the previous way to reach his time small//will this point into the team//here both the initial situation arrives, there are afterwards modified, and the modification needs to be modified all points later, So you also need to enter the team (i.e. the last example given) if (Cheak (x-1,y) &&corrent->time+map[x-1][y]-' 0 ' +1<t[x-1][y]->time) {t[x-1][y]- >time=corrent->time+map[x-1][y]-' 0 ' +1; Q.push (T[x-1][y]); t[x-1][y]->step=4;} if (Cheak (x+1,y) &&corrent->time+map[x+1][y]-' 0 ' +1<t[x+1][y]->time) {T[x+1][y]->time=corrent->time+map[x+1][y]-' 0 ' +1; Q.push (T[x+1][y]); t[x+1][y]->step=3;} if (Cheak (x,y-1) &&corrent->time+map[x][y-1]-' 0 ' +1<t[x][y-1]->time) {t[x][y-1]->time=corrent- >time+map[x][y-1]-' 0 ' +1; Q.push (T[x][y-1]); t[x][y-1]->step=2;} if (Cheak (x,y+1) &&corrent->time+map[x][y+1]-' 0 ' +1<t[x][y+1]->time) {t[x][y+1]->time=corrent- >time+map[x][y+1]-' 0 ' +1; Q.push (t[x][y+1]); t[x][y+1]->step=1;}} int main () {int K,i,order,time;while (cin>>n>>m) {for (k=0;k<n;k++) for (i=0;i<m;i++) {t[k][i]=new Point;cin>>map[k][i];if (map[k][i]== '. ') map[k][i]= ' 0 '; t[k][i]->x=k;t[k][i]->y=i;t[k][i]->time=10000000;t[k][i]->step=-1;} T[0][0]->step=1;bfs (); if (t[n-1][m-1]->time<10000000)//If you can reach your destination {cout<< "It takes" <<t[n-1][ m-1]->time<< "seconds to reach the target position, let me show you the The". <<endl;stack<point> s;point p,last;k=n-1;i=m-1;while (k>=0&&i>=0)//By the last point follow the step of each point to find the route, The last point found is in the stack {p.x=K;p.y=i; S.push (P); order=t[k][i]->step;if (order==4) k+=1;if (order==3) k-=1;if (order==2) i+=1;if (order==1) i-=1;} P=s.top (); S.pop (); Time=1;while (! S.empty ())//output stack {last=p;p=s.top ();cout<< (time++) << "S: (" <<last.x<< "," <<last.y< < ") (" <<p.x<< "," <<p.y<< ")" <<endl;for (k=0;k<map[p.x][p.y]-' 0 '; k++)// Output at a certain point to blame time cout<< (time++) << "S:fight at (" <<p.x<< "," <<p.y<< ")" <<endl; S.pop ();}} elsecout<< "God's help to our poor hero." <<endl;cout<< "FINISH" <<endl;for (k=0;k<n;k++)//delete application space for (i=0;i<m;i++) delete t[k][i];} return 0;}


Test examples:


5 6

. Xx.1.

.. X.9.

2...X.

... Xx.

Xxxxx.




HDU 1026 Ignatius and the Princess I (BFS)

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.