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 72oaround 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.
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.
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.
1 3 S#T 10 10 #S.......# #..#.##.## #.##.##.## .#....##.# ##.##..#.# #..#.##... #......##. ..##.##... #.###...#. #.....###T 0 0
Case #1 destination not reachable Case #2 minimum time = 49 sec
题目大意:给一个迷宫,一个人骑着独轮车在迷宫行进,每前进一格就会将地面染成不同颜色(即轮子有一种颜色扇面与之对应),总共有5种颜色依次循环,在一个格子中,有三种耗时1s的操作,沿前进方向走一步,左转和右转,起始方向向北,给定入口和出口,要求到出口时,地面颜色与初始颜色相同,有路径则求最短路径,否则输出不可能。
#include<iostream> #include<cstring> using namespace std; struct coordinate{ int x; int y; int color; int dre; }; int drg[4][2]={{-1,0},{0,1},{0,-1},{1,0}}; char map[25][25]; int m,n; coordinate en; char co[4][5][25][25]; int t; void buile_map(); int DFS(coordinate *head,coordinate *end); int main() { int re=1,bo; while(cin>>m>>n,m||n) { if(re!=1) cout<<endl; cout<<"Case #"<<re<<endl; buile_map(); coordinate pace[15000]; pace[0].x=en.x,pace[0].y=en.y; t=0,pace[0].color=0,pace[0].dre=0; //统计成员初始化; memset(co,'0',sizeof(co)); bo=DFS(pace,pace+1); if(bo) cout<<"minimum time = "<<t<<" sec"<<endl; else cout<<"destination not reachable"<<endl; re++; } return 0;} void buile_map() { memset(map,0,sizeof(map)); for(int i=0;i<m;i++) for(int j=0;j<n;j++) { cin>>map[i][j]; if(map[i][j]=='S') en.x=i,en.y=j; } } int DFS(coordinate *head,coordinate *end) { if(head==end) return 0; else { t++; coordinate *move=end; for(;head!=end;head++) { for(int i=0;i<4;i++) { if(head->dre==3-i) continue; else if(head->dre==i) { int p,q; p=head->x+drg[i][0]; q=head->y+drg[i][1]; if(p<0||p>=m) continue; if(q<0||q>=n) continue; if(map[p][q]=='#') continue; move->color=(head->color+1)%5; move->x=p,move->y=q; move->dre=i; } else { move->color=head->color; move->x=head->x,move->y=head->y; move->dre=i; } if(co[move->dre][move->color][move->x][move->y]=='1') continue; //判断该位置是否重复路过; co[i][move->color][move->x][move->y]='1'; if(map[move->x][move->y]=='T'&&move->color==0) return 1; move++; } } if(DFS(end,move)) return 1; else return 0; } return 0;}