Ultraviolet A 10047-the monocycle, priority queue + BFS

Source: Internet
Author: User

10047-
Monocycle
3148 35.90% 1014 77.12%

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 105 & page = show_problem & problem = 988

Question type: search, priority queue

Question:

A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five different colors as shown in the figure:

The colored segments make equal angles (72O) At the center. A monoworkflow ist rides this cycle on an Grid
Of square tiles. The tiles have such size that moving forward from the center of one tile to that of the next one makes the wheel rotate exactly 72OAround its own center. The effect is shown in the above figure. When the wheel is at
Center of square 1, the midpoint of the periphery of its blue segment is in touch with the ground. but when the wheel moves forward to the center of the next square (square 2) the midpoint of its white segment touches the ground.

Some of the squares of the grid are blocked and hence the specified ist cannot move to them. the specified ist starts from some square and tries to move to a target square in minimum amount of time. from any square
Either he moves forward to the next square or he remains in the same square but turns 90OLeft or right. Each of these actions requires exactly 1 second to execute. He always starts his ride facing north and with the midpoint of the green
Segment of his wheel touching the ground. In the target square, too, the green segment must be touching the ground but he does not care about the direction he will be facing.

Before he starts his ride, please help him find out whether the destination is reachable and if so the minimum amount of time he will require to reach it.

Translation:

There is a one-wheeled car with 5 different slice color areas on the wheel, and each area is of the same size (72 ° slice ). Ride this car on a square.

Squares are made of square tiles of the same size. The bicycle moves from a tile to an adjacent one, and the wheel turns to 72 °. You can only move toward adjacent top, bottom, left, right tiles. It takes 1 second to move from one tile to the next tile. It takes 1 second to turn the car to 90 °, and it takes 2 seconds to connect to 180 °. White tiles can be taken away, black tiles cannot go (black tiles are replaced by "#", and white tiles are replaced ).

The question must move from the place marked with S to the place marked with T. At the beginning, the wheel was blue on the ground. When it was required to reach the end point, it was also blue on the ground.

If yes, the minimum output time is required. Otherwise, output destination not reachable.

Sample input:

1 3S#T10 10#S.......##..#.##.###.##.##.##.#....##.###.##..#.##..#.##...#......##...##.##...#.###...#.#.....###T0 0

Sample output:

Case #1destination not reachable Case #2minimum time = 49 sec

Analysis and Summary:

At the beginning, I tried to use the greedy idea to search for the shortest path to reach the end point. After arriving at the end point, I checked whether the color was correct. If not, take a few steps around the end point and then walk back to the end to make the color correct. However, there is a problem with this idea and the correct answer cannot be obtained. You can see it.

This is the path of the end point directly searched by BFs. After the path is found, the color is incorrect. Therefore, you can only follow the steps in the original path and then go back to the end point. But the answer is wrong, which is 2 seconds longer than the correct one.

The correct path is as follows:

The color of this path is correct after it reaches the end point.

The above took 2 seconds because it took 2 seconds to go back directly at the end. It would take 180 ° to turn, and it would take 2 seconds to make a 180 ° turn after taking a few steps, which would take 2 seconds. Although the number of steps is the same, there are two more bends than the correct path.

This idea is not feasible. After a long time of meditation, I found that I still did not expect a good method. So temporarily put it down.

Two days later, only the last question is left in this topic.

So the evil Internet searched the problem-solving report and looked at other people's ideas, so they knew that this question would require priority queues, so they knew that they would need multi-dimensional statuses.

Generally, BFs is marked as access in the VIS array after access, and then cannot access this place. However, this topic can be accessed multiple times in a grid, so it cannot be expressed in one dimension.

Each status of this question should be in another direction, color, on a grid, walking direction, and the current color is the same as the previous one, so you cannot access it again.

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <queue> using namespace STD; int dir [4] [2] = {-}, {0,-1}; // top 0, right 1, bottom 2, left 3int m, n, start_x, start_y, end_x, end_y, step; char map [30] [30]; bool vis [30] [30] [4] [5]; // The first two dimensions represent coordinates, and the last two dimensions represent directions and colors. struct node {int X, Y; int color; // green 0, white 1, black 2, Red 3, Blue 4 int time; int dir; // The current direction to friend bool operator <(const node & A, const node & B) {// Overload <build minimum heap return. time> B. time ;}}; node begin, Q, T; priority_queue <node> que; void BFS () {While (! Que. empty () que. pop (); que. push (BEGIN); vis [begin. x] [begin. y] [begin. dir] [begin. color] = true; while (! Que. empty () {q = que. top (); que. pop (); For (INT I = 0; I <4; ++ I) {int dx = Q. X + dir [I] [0]; int DY = Q. Y + dir [I] [1]; if (dx> = 0 & DX <M & dy> = 0 & dy <n & map [dx] [dy]! = '#') {If (Q. dir = I) {T. time = Q. time + 1; T. dir = I; T. X = DX, T. y = Dy; T. color = (Q. color + 1) % 5;} else {If (ABS (Q. dir-I) = 2) T. time = Q. time + 2; else t. time = Q. time + 1; T. dir = I; T. X = Q. x, t. y = Q. y; T. color = Q. color;} If (! Vis [T. x] [T. y] [I] [T. color]) {If (T. X = end_x & T. y = end_y & T. color = 0) {printf ("minimum time = % d sec \ n", T. time); return;} que. push (t); vis [T. x] [T. y] [I] [T. color] = true ;}}} printf ("destination not reachable \ n");} int main () {# ifdef local freopen ("input.txt", "r ", stdin); # endif int CAS = 1; while (~ Scanf ("% d % * C", & M, & N) & M & N) {bool isfind_s = false, isfind_t = false; memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <m; ++ I) {gets (Map [I]); If (! Isfind_s |! Isfind_t) {for (int K = 0; k <strlen (Map [I]); ++ K) {If (Map [I] [k] ='s ') {start_x = I, start_y = K; isfind_s = true;} If (Map [I] [k] = 'T') {end_x = I, end_y = K; isfind_t = true ;}}} begin. X = start_x, begin. y = start_y, begin. color = 0, begin. time = 0; begin. dir = 0; If (CAS! = 1) printf ("\ n"); printf ("case # % d \ n", CAS ++); BFS ();} return 0 ;}

Additional test exampleInput:

5 10
. S ........
..##.....#
... #... T...
..#.......
..#.......
15 15
S ......#.......
.......#.......
... #... T.
......#..#.....
.....#...#.....
....#.....#...#
....#.#.#.#....
....#..#...#...
...#..##.#.#...
...#...#....#..
..#..#.#....#..
..#.#..#.....#.
.##..#.#.....#.
...#...#.....#.
#......#.......
1 10
. S... t ..
1 3
S # T
10 10
# S .......#
#..#.##.##
#.##.##.##
.#....##.#
##.##..#.#
#..#.##...
#......##.
..##.##...
#.###...#.
# ...... ### T
5 5
# S #..
#.#..
#.###
#... T
#####
10 10
S .........
..........
..........
..........
..........
..........
..........
..........
..........
... T.
3 3
St #
##.
.#.
6 6
#.#...
#. S .#.
#####.
#..#..
# T ##..
......
0 0

Additional test sample output:

Case #1
Minimum time = 19 Sec

Case #2
Minimum time = 82 Sec

Case #3
Minimum time = 13 sec

Case #4
Destination not reachable

Case #5
Minimum time = 49 Sec

Case #6
Minimum time = 17 sec

Case #7
Minimum time = 30 sec

Case #8
Minimum time = 14 sec

Case #9
Minimum time = 30 sec


-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double



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.