HDU 1026 Ignatius and the princess I [priority queue + BFS]

Source: Internet
Author: User
Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1026 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 29096 # Problem/dignatius and the princess I

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 10006 accepted submission (s): 3004
Special Judge


Problem descriptionthe princess has been abducted by the Beelzebub feng5166, our hero Ignatius has 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 is a n * m two-dimensional
Array which left-top corner is () and right-bottom corner is (N-1, M-1 ). ignatius enters at (0, 0), and the door to feng5166's room is at (N-1, M-1), that is our target. there are some monsters in the castle, if Ignatius meet them, he has to kill them. here
Is some rules:

1. ignatius can only move in four directions ctions (Up, down, left, right), 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 is a trap, Ignatius shocould 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. you may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.


Inputthe input contains several test cases. each test case starts with a line contains two numbers N and M (2 <= n <= 100,2 <= m <= 100) which indicate the size of the labyrinth. then a n * m two-dimen1_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 shoshould output "God please help our poor hero. "If Ignatius can't reach the target position, or you shoshould output" it takes n seconds to reach the target position, let me show you the way. "(n is the minimum seconds), and tell our hero
The whole path. Output A line contains "finish" after each test case. If there are more than one path, any one is 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 13 seconds to reach the target position, let me show you the way.1s:(0,0)->(1,0)2s:(1,0)->(1,1)3s:(1,1)->(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 14 seconds to reach the target position, let me show you the way.1s:(0,0)->(1,0)2s:(1,0)->(1,1)3s:(1,1)->(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 please help our poor hero.FINISH
 


Authorignatius. L

Question:

Give you an N * m graph. You need to go from the first vertex to the last vertex [from the upper left corner to the lower right corner]
You can only go in the upper, lower, and left directions.


.: Indicates you can go
 
X: indicates a wall.


N: Here is a monster. When the monster is defeated, n


Each step takes 1.


If this parameter can be reached, the minimum output time and steps of each step are shown.
Cannot reach output...
Sample output.


Note:Make sure that there are no monsters at the starting point, and the ending point is not a wall. [That is to say, there may be monsters at the end]

Algorithm: priority queue + BFS [nature Dijkstra] Train of Thought: Very bare, the shortest short circuit, but not familiar with priority queue, using their own stupid method wrong for a long time.

Positive Solution:
Because the priority queue + BFS is used to facilitate the output path Search from the end. It can also start from the starting point, but it is not convenient. Either the recursive output path is clicked to open the link or an array is defined for maintenance, making the simple questions that are originally complicated more complicated.
Defines a priority queue, including the location and the shortest time from the current location to the destination. The priority queue ensures that the team leaves the queue in a short time...
// Define a priority queue: if the number of nodes that have joined the queue is less than the first time, the first node that has reached the end is the result struct node {int X, Y; // int time currently reached; // time consumed bool operator <(const node & B) const {return B. time <time ;}};

The reload of the priority queue is not very clear... I understand this as follows. Every time a new vertex B is added, regardless of their X and Y, that is, the default definition of X and Y according to the common queue, first-in-first-out
Time is given priority. B. Time is compared with every time in the original queue. [traverse from first to first.] If B. time <time"B insert to the position currently traversed". For more information, see the following. Because the priority queue ensures that the first team is out of the queue based on a small amount of time, the first traversal to the starting point must be the shortest path. There may be other routes in the future, but they will not be better than this one. [Special Judge]
Next let's consider the output: Define a pioneer struct to record the position of the previous vertex of each vertex.Note:: Because the output path is searched from the end point to the start point to facilitate the search, the points recorded in the pioneer are actually the next point in the process.
Click to open the link.My initial error ideas:
I didn't expect to use the priority queue for search at first. Because I had previously done BFS water dumping problem poj 3414 pots [BFS simulated water dumping problem], I also wanted to simulate the output path, which was very similar. In fact, it was wrong... Then, follow the instructions in this question. We also considered that there would be multiple routes to arrive, but we were prepared to perform a direct brute force search for all the points only when we thought of a few 100*100 points. At first, I thought it would be better to compare it with the original one. [I thought that method would arrive at the end multiple times.] In fact, I marked it when I joined the team, but I didn't mark it when I went out, therefore, you can only reach the end point once at most. You cannot reach the end point multiple times at all, and the minimum Update time is allowed. However, the sample code came out, and I couldn't think of it myself.
Later, I consulted nanchengbian, and he pointed out the problems mentioned above, making it impossible to update the minimum time. Because according to the above statement, the end point can only be in and out of the queue once. Then I tried to simulate it with the shortest path, but I was still preparing to modify it based on the above, so I also marked it for every team, this ensures that the end point can be traversed multiple times, but this will not solve the problem of jumping out of BFS, so I think that each point can be taken at most once by the top, bottom, and right points, is it okay to mark each vertex to join the team up to four times? The result is still wa... Think about it: Although I marked the team four times, the four times may all arrive at the same point, but they are not separated from each other, the shortest time of the above update cannot be solved.
Therefore: In the end, only the shortest path can be traversed.
This is a very bare question. I was just planning to practice it, but it took a long time. Cainiao should work hard at fighting !!! Come on
Code:

# Include <stdio. h> # include <string. h >#include <queue >#include <algorithm> # include <iostream> using namespace STD; const int maxn = 110; const int INF = maxn * 10; int map [maxn] [maxn]; // record chart int vis [maxn] [maxn]; // Mark into char STR [maxn]; int n, m; int dir [4] [2] = {, 0,-1,-,}; // defines the priority queue: For the nodes that have joined the queue, when the first node is out of the queue, the first node that arrives at the end is the result struct node {int X, Y; // The Int time at which the current node arrives; // time-consuming bool operator <(const node & B) const {Return B. time <time ;}}; // The precursor of each vertex. Because it is a reverse search, the record is actually the next vertex of the current vertex, struct pre {int PX, Py ;} pre [maxn] [maxn]; void BFS () {node now, next; priority_queue <node> q; while (! Q. empty () Q. pop (); now. X = N; now. y = m; // start from the end point now. time = map [N] [m]; // note: the final point may also contain the monster pre [N] [M]. px =-1; // output boundary Q. push (now); memset (VIS, 0, sizeof (VIS); // for the convenient and fast output path, find vis [N] [m] = 1 from the end point to the start point; // mark the end point for the while (! Q. empty () {now = Q. top (); q. pop (); If (now. X = 1 & now. y = 1) // once it reaches the starting point {printf ("it takes % d seconds to reach the target position, let me show you the way. \ n ", now. time); int time = 1; int x = now. x, Y = now. y; // The current position int Nx = pre [x] [Y]. px, NY = pre [x] [Y]. PY; // The next position while (pre [x] [Y]. px! =-1) // keep searching for the precursor {printf ("% DS :( % d, % d)-> (% d, % d) \ n", time ++, x-1, Y-1, nx-1, ny-1); While (Map [NX] [NY] --) // if there is a monster {printf ("% DS: fight at (% d, % d) \ n ", time ++, nx-1, ny-1);} X = NX; y = NY; // continue to search for the next vertex Nx = pre [x] [Y]. px, NY = pre [x] [Y]. PY;} printf ("Finish \ n"); return; // end} For (INT I = 0; I <4; I ++) {next. X = now. X + dir [I] [0]; next. y = now. Y + dir [I] [1]; If (Map [next. x] [next. y]> = 0 &&! Vis [next. x] [next. y]) // The current vertex can be taken, and the node has not been in the queue. {vis [next. x] [next. y] = 1; // mark next. time = now. time + 1 + map [next. x] [next. y]; Pre [next. x] [next. y]. px = now. x; // pre [next. x] [next. y]. py = now. y; q. push (next) ;}} printf ("God please help our poor hero. \ n "); // printf (" Finish \ n "); return;} int main () {While (scanf (" % d ", & N, & M )! = EOF) {gets (STR); For (INT I = 0; I <= n + 1; I ++) // Add edge around for (Int J = 0; j <= m + 1; j ++) map [I] [J] =-1; char C; For (INT I = 1; I <= N; I ++) // when outputting the data, note that {for (Int J = 1; j <= m; j ++) {scanf ("% C ", & C); If (C! = 'X') {If (C = '. ') map [I] [J] = 0; else map [I] [J] = C-'0' ;}} gets (STR) ;}bfs ();} return 0 ;}
 

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.