Ignatius 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 Description
The 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-dimen1_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.
Input
The 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.
Output
For 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.
XXXXX1
5 6
. XX...
.. XX1.
2 .. X.
... XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1 s :( 0, 0)-> (1, 0)
2 s :( 1, 0)-> (1, 1)
3 s: (1, 1)-> (2, 1)
4 s :()->)
5s :()->)
6 s: (2, 3)-> (1, 3)
7 s: (1, 3)-> (1, 4)
8 s: fight at (1, 4)
9 s: fight at (1, 4)
10 s: (1, 4)-> (1, 5)
11 s :()->)
12 s :( 2,5)-> (3,5)
13 s :( 3, 5)-> (4, 5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1 s :( 0, 0)-> (1, 0)
2 s :( 1, 0)-> (1, 1)
3 s: (1, 1)-> (2, 1)
4 s :()->)
5s :()->)
6 s: (2, 3)-> (1, 3)
7 s: (1, 3)-> (1, 4)
8 s: fight at (1, 4)
9 s: fight at (1, 4)
10 s: (1, 4)-> (1, 5)
11 s :()->)
12 s :( 2,5)-> (3,5)
13 s :( 3, 5)-> (4, 5)
14 s: fight at (4, 5)
FINISH
God please help our poor hero.
FINISH
Author
Ignatius. 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]
Ignatius 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 Description
The 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-dimen1_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.
Input
The 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.
Output
For 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.
XXXXX1
5 6
. XX...
.. XX1.
2 .. X.
... XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1 s :( 0, 0)-> (1, 0)
2 s :( 1, 0)-> (1, 1)
3 s: (1, 1)-> (2, 1)
4 s :()->)
5s :()->)
6 s: (2, 3)-> (1, 3)
7 s: (1, 3)-> (1, 4)
8 s: fight at (1, 4)
9 s: fight at (1, 4)
10 s: (1, 4)-> (1, 5)
11 s :()->)
12 s :( 2,5)-> (3,5)
13 s :( 3, 5)-> (4, 5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1 s :( 0, 0)-> (1, 0)
2 s :( 1, 0)-> (1, 1)
3 s: (1, 1)-> (2, 1)
4 s :()->)
5s :()->)
6 s: (2, 3)-> (1, 3)
7 s: (1, 3)-> (1, 4)
8 s: fight at (1, 4)
9 s: fight at (1, 4)
10 s: (1, 4)-> (1, 5)
11 s :()->)
12 s :( 2,5)-> (3,5)
13 s :( 3, 5)-> (4, 5)
14 s: fight at (4, 5)
FINISH
God please help our poor hero.
FINISH
Author
Ignatius. 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]
Ideas:
Very bare, but not familiar with the priority queue, wrong for a long time using your stupid method.
Solution:
Because the priority queue + BFS is used to facilitate the output path, the search starts from the end point.
It can also start from the starting point, but it is not convenient, or the recursive output path is clicked to open the link
Either define an array for maintenance, making 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
Then, insert "B to the current traversal location". For more information, see this article.
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 analysis:
I didn't want to use the priority queue for search at first. Because I had previously done BFS dumping problem POJ 3414 Pots [bfs simulated dumping problem], I also wanted to simulate the output path.
It feels like, but it's actually 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 reach the end multiple times]
In fact, I marked it when I joined the team, but I didn't mark it when I went out, so I could only reach the end point once at most. It was impossible to reach the end point multiple times and update the minimum time.
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 short circuit, but I was still ready to modify it on the basis of the above. So I also marked every dot-out team, which ensured that the terminal could be traversed multiple times.
However, this will not solve the problem of jumping out of BFS, so I think that each point can be taken once at most by the top, bottom, and right points. Is it okay to mark each point to join 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 ;}