Ultraviolet A 10047-the monocycle

Source: Internet
Author: User

Problem A: the monocycle


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.

Input

The input may contain in multiple test cases.

The first line of each test case contains two integersMAndN(,)
Giving the dimensions of the Grid. Then follows the description of the grid inMLines
NCharacters each. The character'#'Will indicate a blocked square, all other squares are free. The starting location of the specified ist is marked'SAnd the target is marked'T'. The input terminates with two zeros
MAndN.

Output

For each test case in the input first print the test case number on a separate line as shown in the sample output. if the target location can be reached by the specified ist print the minimum amount of time (in seconds) required to reach it exactly in the format
Shown in the sample output, otherwise, print''Destination not reachable".

Print a blank line between two successive test cases.

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

Sample output
Case #1destination not reachable Case #2minimum time = 49 sec
For the shortest time from S to T, it is required to set off from S to face north, and the wheels are green to Earth. It doesn't matter where the face is, and only '#' cannot be reached.
Calculation of time: in-situ left turn or right turn or forward further time + 1; you can use common queue or priority queue BFs,
Each second records the current time, coordinates, the color, orientation, and state parameters of the wheels on the ground. There are also many conditions.
Wrong has made many low-level errors. Make multiple equal signs and make everything messy. The more upset the question, the more careful ~~~~ (>_< )~~~~
# Include <stdio. h> # include <string. h> struct node {int time, X, Y, color, dire; // color 0.1.2.3.4. The initial green value is 0, and the direction is 0 north (top) 1 East (right) 2 South (bottom) 3 West (left)} Q [13000]; 25*25*5*4 = 12500 States char s [30] [30]; int sum = 0, top, tail, F, n, m, visit [30] [30] [5] [4], move [4] [2] = {-, 0,-1 }; move and the specified direction correspond to int BFS () {int PX, Py; If (S [Q [Top]. x] [Q [Top]. y] = 'T') & (Q [Top]. color = 0) {f = 1; return 0;} PX = Q [Top]. X + move [Q [Top]. dire] [0]; py = Q [Top]. Y + move [Q [Top]. dire] [1]; If (PX> = 0) & (PX <n) & (PY> = 0) & (PY <m) & (visit [PX] [py] [(Q [Top]. color + 1) % 5] [Q [Top]. Dire] = 0) & (s [PX] [py]! = '#') // Forward a grid {visit [PX] [py] [(Q [Top]. color + 1) % 5] [Q [Top]. dire] = 1; ++ tail; Q [tail]. time = Q [Top]. time + 1; Q [tail]. X = px; Q [tail]. y = py; Q [tail]. color = (Q [Top]. color + 1) % 5; Q [tail]. dire = Q [Top]. dire;} If (visit [Q [Top]. x] [Q [Top]. y] [Q [Top]. color] [(Q [Top]. dire + 1) % 4] = 0) & (s [Q [Top]. x] [Q [Top]. y]! = '#') // Right {visit [Q [Top]. x] [Q [Top]. y] [Q [Top]. color] [(Q [Top]. dire + 1) % 4] = 1; ++ tail; Q [tail]. time = Q [Top]. time + 1; Q [tail]. X = Q [Top]. x; Q [tail]. y = Q [Top]. y; Q [tail]. color = Q [Top]. color; Q [tail]. dire = (Q [Top]. dire + 1) % 4;} If (visit [Q [Top]. x] [Q [Top]. y] [Q [Top]. color] [(Q [Top]. dire + 3) % 4] = 0) & (s [Q [Top]. x] [Q [Top]. y]! = '#') // Turn to the left {visit [Q [Top]. x] [Q [Top]. y] [Q [Top]. color] [(Q [Top]. dire + 3) % 4] = 1; ++ tail; Q [tail]. time = Q [Top]. time + 1; Q [tail]. X = Q [Top]. x; Q [tail]. y = Q [Top]. y; Q [tail]. color = Q [Top]. color; Q [tail]. dire = (Q [Top]. dire + 3) % 4 ;}++ top; If (top <= tail) BFS (); Return 0 ;}int main () {int I, J; while (scanf ("% d", & N, & M), n) {If (SUM) printf ("\ n "); // The last group of data does not wrap for (I = 0; I <n; I ++) scanf ("% s", & S [I]); Top = 1; tail = 1; F = 0; memset (visit, 0, sizeof (visit); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) if (s [I] [J] = 's') {q [1]. time = 0; Q [1]. color = 0; Q [1]. dire = 0; Q [1]. X = I; Q [1]. y = J; visit [I] [J] [0] [0] = 1; break;} BFS (); printf ("case # % d \ n ", + + sum); If (F = 0) printf ("destination not reachable \ n"); else printf ("minimum time = % d sec \ n ", Q [Top]. time) ;}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.