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:
![](http://uva.onlinejudge.org/external/100/p10047a.gif)
The colored segments make equal angles (72o) at the center. A monocyclist 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 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.
![](http://uva.onlinejudge.org/external/100/p10047b.gif)
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 (,
) giving the dimensions of the grid. Then follows the description of the grid inM 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 3 S#T 10 10 #S.......# #..#.##.## #.##.##.## .#....##.# ##.##..#.# #..#.##... #......##. ..##.##... #.###...#. #.....###T 0 0
Sample Output
Case #1 destination not reachable Case #2 minimum time = 49 sec
题意:
一自行车的轮子有5种不同颜色,自行车每1秒,要么 转下一个格子,要么 左转,要么 右转;一开始 的方位是向北,颜色为绿,到达目标格时,必须触底颜色为绿,但朝向无限制。求到达目标格的最短时间。
</pre><p></p><pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 30;
const int color = 5;
const int dir = 4;
const int MIN = -1;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, kase;
char a[maxn][maxn];
int vis[maxn][maxn][dir][color];
struct node{
int x, y, d, c;
int step;
node(int x, int y, int d, int c, int step):
x(x), y(y), d(d), c(c), step(step) {}
};
int bfs(int sx, int sy, int ex, int ey){
memset(vis, 0, sizeof(vis));
queue<node> q;
q.push(node(sx, sy, 0, 0, 0));
vis[sx][sy][0][0] = 1;
while(!q.empty()) {
node t = q.front();
q.pop();
if(t.x == ex && t.y == ey && t.c == 0){
return t.step;
}
for(int i=0; i<4; i++) {
if(i == t.d){
int x = t.x+dx[i];
int y = t.y+dy[i];
int c = (t.c+1)%color;
int s = t.step+1;
if (x >= 0 && x < n && y >=0 && y <m) {
if(a[x][y] !='#' && vis[x][y][i][c] == 0)
{
vis[x][y][i][c] = 1;
q.push(node(x, y, i, c, s));
}
}
}
else if((t.d+1)%dir == i || (t.d+dir-1)%dir == i){
if(vis[t.x][t.y][i][t.c]==0)
{
vis[t.x][t.y][i][t.c] = 1;
q.push(node(t.x, t.y, i, t.c, t.step+1));
}
}
}
}
return MIN;
}
int main(){
int sx, sy, ex, ey;
kase = 0;
while(scanf("%d%d", &n, &m) != EOF){
if(n == 0 && m == 0) break;
if (kase)
printf("\n");
printf("Case #%d\n", ++kase);
for(int i=0; i<n; i++) scanf("%s", a[i]);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(a[i][j] == 'S') { sx = i; sy = j; }
else if(a[i][j] == 'T') { ex = i; ey = j; }
}
}
int k = bfs(sx, sy, ex, ey);
if(k == MIN) printf("destination not reachable\n");
else printf("minimum time = %d sec\n",k);
}
return 0;
}