首页 > 代码库 > uva 10047 - The Monocycle

uva 10047 - The Monocycle

  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 monocyclist rides this cycle on an $M \times N$ 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 72o around 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 cyclist cannot move to them. The cyclist 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 90o left 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 multiple test cases.

The first line of each test case contains two integers M and N ($1 \le M$, $N \le 25$) giving the dimensions of the grid. Then follows the description of the grid in M lines of N characters each. The character `#‘ will indicate a blocked square, all other squares are free. The starting location of the cyclist is marked by `S‘ and the target is marked by `T‘. The input terminates with two zeros for M and N.

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 cyclist 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 vint 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坐标;d方向;c颜色;s步数    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])//向右转        {            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])//向左转        {            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] != #)//直走        {            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%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;}