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 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 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 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 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 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 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 3 S#T 10 10 #S.......# #..#.##.## #.##.##.## .#....##.# ##.##..#.# #..#.##... #......##. ..##.##... #.###...#. #.....###T 0 0
Sample Output
Case #1 destination not reachable Case #2 minimum time = 49 sec
一道四维BFS题,看起来比较乱,需要用vv数组记录不同方向,颜色,位置是否被遍历过,看出来就比较容易做。不过还是debug了很久。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<ctype.h>
#include<algorithm>
#include<string>
#define PI acos(-1.0)
#define maxn 30
#define maxm 6
#define INF 1<<25
typedef long long ll;
using namespace std;
char mmap[maxn][maxn];
int vv[maxn][maxn][maxm][maxm];
int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
struct block
{
int step;
int x,y;
int col;
int dirct;
} st,s1,s2;
int xx,yy,enx,eny;
int jud=0;
int cango(int x,int y,int a,int b)
{
if(x>=0&&x<xx&&y>=0&&y<yy&&(mmap[x][y]=='.'||mmap[x][y]=='S'||mmap[x][y]=='T')&&!vv[x][y][a][b])
return 1;
return 0;
}
int bfs()
{
st.step=0;
st.col=0;
st.dirct=0;
queue<block>q;
q.push(st);
vv[st.x][st.y][0][0]=1;
while(!q.empty())
{
s1=q.front();
q.pop();
s2.dirct=s1.dirct;
s2.x=s1.x+dir[s1.dirct][0];
s2.y=s1.y+dir[s1.dirct][1];
s2.step=s1.step+1;
s2.col=(s1.col+1)%5;
if(cango(s2.x,s2.y,s2.col,s2.dirct))
{
if(s2.x==enx&&s2.y==eny&&s2.col==0)
{
printf("minimum time = %d sec\n",s2.step);
jud=1;
break;
}
vv[s2.x][s2.y][s2.col][s2.dirct]=1;
q.push(s2);
}
s2.x=s1.x;
s2.y=s1.y;
s2.step=s1.step+1;
s2.col=s1.col;
s2.dirct=(s1.dirct+1)%4;
if(cango(s2.x,s2.y,s2.col,s2.dirct))
{
vv[s2.x][s2.y][s2.col][s2.dirct]=1;
q.push(s2);
}
s2.x=s1.x;
s2.y=s1.y;
s2.step=s1.step+1;
s2.col=s1.col;
s2.dirct=(s1.dirct+3)%4;
if(cango(s2.x,s2.y,s2.col,s2.dirct))
{
vv[s2.x][s2.y][s2.col][s2.dirct]=1;
q.push(s2);
}
}
return 0;
}
int main()
{
int ii=0;
while(scanf("%d%d",&xx,&yy))
{
jud=0;
memset(vv,0,sizeof(vv));
memset(mmap,0,sizeof(mmap));
ii++;
if(xx==0&&yy==0)
break;
if(ii!=1)
printf("\n");
for(int i=0; i<xx; i++)
{
scanf("%s",mmap[i]);
for(int j=0; j<yy; j++)
{
if(mmap[i][j]=='S')
{
st.x=i;
st.y=j;
}
if(mmap[i][j]=='T')
{
enx=i;
eny=j;
}
}
}
printf("Case #%d\n",ii);
bfs();
if(jud==0)
printf("destination not reachable\n");
}
}