10047 - The Monocycle
Time limit: 3.000 secondsA 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 ve different colors as shown in the gure:
The colored segments make equal angles (72o
) at the center. A monocyclist rides this cycle on an
M 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 gure. 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 nd 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 rst line of each test case contains two integers M and N (1 M, N 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 rst 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
与一般的迷宫不同的是这个题目除了迷宫还有状态
题目意思有一个独轮车在迷宫中走,独轮车的轮子平分成5段,分别有5种颜色
每走一个格子,朝向地面的颜色变一种。
独轮车可以在1s前进一格或者原地左转或右转90度
最开始在S处面向上(北)车轮是颜色A最终在T处车轮颜色A
如果路径不存在另作输出
因此记录最短路步数的矩阵变成了4维 x坐标y坐标d面向方向c颜色
这题居然wa了1发输出,还不是因为题目设计的坑,wa了case 0……
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int M=30;
char ti[M][M];
int isu[M][M][4][5];
struct NODE
{
NODE(int xx,int yy,int dd,int cc)
{
x=xx;y=yy;d=dd;c=cc;
}
int x,y,d,c;
};
queue<NODE> re;
int bfs(int n,int m)
{
while(!re.empty())
re.pop();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(ti[i][j]=='S')
{
re.push(NODE(i,j,0,0));
isu[i][j][0][0]=1;
break;
}
while(!re.empty())
{
NODE t=re.front();
re.pop();
int tx=t.x,ty=t.y,td=t.d,tc=t.c;
int x,y,c;
if(td==0) //直走的方向
x=tx-1,y=ty,c=(tc+1)%5;
else if(td==1)
x=tx,y=ty+1,c=(tc+1)%5;
else if(td==2)
x=tx+1,y=ty,c=(tc+1)%5;
else// td==3
x=tx,y=ty-1,c=(tc+1)%5;
if(ti[x][y]&&ti[x][y]!='#') //直行
{
if(ti[x][y]=='T'&&c%5==0)
return isu[tx][ty][td][tc];
else if(!isu[x][y][td][c])
{
re.push(NODE(x,y,td,c));
isu[x][y][td][c]=isu[tx][ty][td][tc]+1;
}
}
if(!isu[tx][ty][(td+1)%4][tc]) //右转
{
re.push(NODE(tx,ty,(td+1)%4,tc));
isu[tx][ty][(td+1)%4][tc]=isu[tx][ty][td][tc]+1;
}
if(!isu[tx][ty][(td+3)%4][tc]) //左转
{
re.push(NODE(tx,ty,(td+3)%4,tc));
isu[tx][ty][(td+3)%4][tc]=isu[tx][ty][td][tc]+1;
}
}
return 0;
}
int main()
{
int n,m;
int ca=0;
while(1)
{
scanf("%d%d",&n,&m);
if(!m&&!n)
break;
memset(ti,0,sizeof(ti));
memset(isu,0,sizeof(isu));
for(int i=1;i<=n;i++)
scanf("%s",ti[i]+1);
int ans=bfs(n,m);
if(ca) //只有组间有空格
printf("\n");
printf("Case #%d\n",++ca);
if(!ans)
printf("destination not reachable\n");
else
printf("minimum time = %d sec\n",ans);
}
return 0;
}