uva 10047 - The Monocycle

来源:互联网 发布:上海动悦网络靠谱吗 编辑:程序博客网 时间:2024/04/29 13:48


  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 inM lines ofN 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 forM 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
求从S到T的最短时间,要求从s出发时面朝北,轮子的绿色着地,到T时仍旧绿色着地,面朝哪无所谓,只有‘#’不可到达。
时间的计算:原地左转或右转或向前进一步时间+1;可以用普通队列或优先队列bfs,
每一秒记录当前的时间,坐标,轮子着地颜色,朝向,状态参量比较多,条件也比较多
一不留神打错了,wrong了好多次,各种低级错误。多打个等号,乱七八糟什么都有,越是心烦的题目越要仔细~~~~(>_<)~~~~ 
#include <stdio.h>#include <string.h>struct node{int time,x,y,color,dire; //颜色0.1.2.3.4。初始绿色为0,方向0 北(上) 1 东(右) 2南(下) 3西(左)}q[13000];  25*25*5*4=12500种状态char s[30][30];int sum=0,top,tail,f,n,m,visit[30][30][5][4],move[4][2]={-1,0,0,1,1,0,0,-1};move和刚才规定的方向要对应int bfs(){int px,py; if ((s[q[top].x][q[top].y]=='T')&&(q[top].color==0)) {f=1; return 0;} px=q[top].x+move[q[top].dire][0]; py=q[top].y+move[q[top].dire][1]; if ((px>=0)&&(px<n)&&(py>=0)&&(py<m)&&(visit[px][py][(q[top].color+1)%5][q[top].dire]==0)&&(s[px][py]!='#')) //前进一格 {visit[px][py][(q[top].color+1)%5][q[top].dire]=1;   ++tail;  q[tail].time=q[top].time+1;  q[tail].x=px; q[tail].y=py;  q[tail].color=(q[top].color+1) % 5;  q[tail].dire=q[top].dire; } if ((visit[q[top].x][q[top].y][q[top].color][(q[top].dire+1)%4]==0)&&(s[q[top].x][q[top].y]!='#')) //向右转 {visit[q[top].x][q[top].y][q[top].color][(q[top].dire+1)%4]=1; ++tail;  q[tail].time=q[top].time+1;  q[tail].x=q[top].x; q[tail].y=q[top].y;  q[tail].color=q[top].color;  q[tail].dire=(q[top].dire+1)%4; } if ((visit[q[top].x][q[top].y][q[top].color][(q[top].dire+3)%4]==0)&&(s[q[top].x][q[top].y]!='#'))  //向左转 {visit[q[top].x][q[top].y][q[top].color][(q[top].dire+3)%4]=1; ++tail;  q[tail].time=q[top].time+1;  q[tail].x=q[top].x; q[tail].y=q[top].y;  q[tail].color=q[top].color;  q[tail].dire=(q[top].dire+3)%4; } ++top; if (top<=tail) bfs(); return 0;}int main(){int i,j; while (scanf("%d%d",&n,&m),n) {if (sum) printf("\n");  //最后一组数据不换行  for (i=0;i<n;i++)  scanf("%s",&s[i]);  top=1; tail=1; f=0;  memset(visit,0,sizeof(visit));  for (i=0;i<n;i++)  for (j=0;j<m;j++)  if (s[i][j]=='S')  {q[1].time=0; q[1].color=0; q[1].dire=0;   q[1].x=i;    q[1].y=j;   visit[i][j][0][0]=1;   break;  }  bfs();  printf("Case #%d\n",++sum);  if (f==0) printf("destination not reachable\n");       else printf("minimum time = %d sec\n",q[top].time); } return 0;}