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 the center of square 1, the mid point 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 mid point 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 mid point 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 inMLinesNCharacters 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 zerosMAndN.
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
# Include <iostream> # include <stack> # include <cstring> # include <cstdio> # include <string> # include <algorithm> # include <queue> # include <set> # include <map> # include <fstream> # include <stack> # include <list> # include <sstream> # include <cmath> using namespace STD; # define MS (ARR, Val) memset (ARR, Val, sizeof (ARR) # define MC (DEST, Src) memcpy (DEST, SRC, sizeof (SRC )) # define N 30 # define INF 0x3fffffff # define vin T vector <int> # define setint set <int> # define mint Map <int, int> # define lint list <int> # define Sch stack <char> # define qch queue <char> # define Sint stack <int> # define qint queue <int> bool tag [n] [N] [4] [5]; // 4 (0: N; 1: E; 2: S; 3: W); 5 (0: green; 1: black; 2red; 3: Blue; 4: white) int dir [4] [2] = {-1, 0, 0, 1, 1, 0, 0,-1}; // (n, E, S, w) Char grid [N] [N]; int ans; struct node {int I, j, D, C, S; // I, j coordinate; D direction; c color; s step node (INT I = 0, Int J = 0, int D = 0, int C = 0, int S = 0): I (I), J (J), D (D ), C (c), S (s) {} void set (int I, Int J, int D, int C, int s) {This-> I = I; this-> J = J; this-> d = D; this-> C = C; this-> S = s ;}} s; queue <node> que; void BFS () {MS (TAG, 0); que. push (s); tag [S. i] [S. j] [S. d] [S. c] = true; int I, j, D, C; while (! Que. empty () {node T = que. front (); que. pop (); If (grid [T. i] [T. j] = 'T' & T. C = 0) {ans = T. s; while (! Que. Empty () {que. Pop ();} return;} d = (T. D + 1) % 4; If (! Tag [T. i] [T. j] [d] [T. c]) // turn right {que. push (node (T. i, T. j, D, T. c, T. S + 1); tag [T. i] [T. j] [d] [T. c] = true;} d = (T. d + 3) % 4; If (! Tag [T. i] [T. j] [d] [T. c]) // turn left {que. push (node (T. i, T. j, D, T. c, T. S + 1); tag [T. i] [T. j] [d] [T. c] = true;} I = T. I + dir [T. d] [0]; j = T. J + dir [T. d] [1]; C = (T. c + 1) % 5; If (! Tag [I] [J] [T. D] [c] & grid [I] [J]! = '#') // Go straight to {que. push (node (I, j, T. d, C, T. S + 1); tag [I] [J] [T. d] [c] = true ;}}int main () {int m, n, I, j, c = 0; MS (grid [0], '#'); While (scanf ("% d", & M, & N), M & N) {for (I = 1; I <= m; I ++) {scanf ("% s", grid [I] + 1 ); grid [I] [0] = grid [I] [n + 1] = '#';} MS (grid [I], '#'); I = j = 1; while (true) {If (grid [I] [J] = 's') {S. set (I, j, 0, 0, 0); break;} J ++; If (j> N) {I ++; j = 1 ;}} ans = 0; BFS (); If (c) putchar ('\ n'); printf ("case # % d \ n", ++ C); If (ANS) printf ("minimum time = % d sec \ n", ANS); else puts ("destination not reachable");} return 0 ;}