UVA10047

来源:互联网 发布:天猫双十一直播数据 编辑:程序博客网 时间:2024/05/16 08:50

  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 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 3S#T10 10#S.......##..#.##.###.##.##.##.#....##.###.##..#.##..#.##...#......##...##.##...#.###...#.#.....###T0 0

Sample Output 

Case #1destination not reachable Case #2minimum time = 49 sec
分析:本题是一道求解最最短路的问题,不过一开始看到这道题的时候会觉得状态太多,而且太乱了,不知道怎么下手。其实1 <= M,N<= 25,颜色有5种,朝向有4种,所以最多的状态数为25*25*5*4 = 12500种,所以可以采用BFS求最短路。只不过有一个地方需要注意,就是状态转移数组dx[][],dy[][],dc[],dd[],因为当目前轮子的朝向分别为N,W,S,E时,从目前轮子的朝向往前,左,右转移的时候位置状态转移改变的量不同,所以需要将这四组转移的该变量都列举出来。开始的时候就是因为这个我没有考虑到这四种朝向的转移的该变量不同,所以没有卡住了。
#include<cstdio>#include<queue>#include<string.h>#include<algorithm>using namespace std;typedef struct{    short x, y;    short  color;    short  dir;}Cond;Cond s, e;//定义起始状态和终止状态queue<Cond> q;int M, N;int s_x, s_y, e_x, e_y;int dx[4][3] = {{-1, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 0}}, dy[4][3] = {{0, 0, 0}, {-1, 0, 0}, {0, 0, 0}, {1, 0, 0}};int dc[3] = {1, 0, 0}, dd[3] = {0, -1, 1};//当朝向为N,W,E,S时,分别应该怎样变换状态char maze[25][25];//bool Rem[25][25][5][4];//记录下所有的状态是否已经访问过了int  Step[25][25][5][4];//记录下到达每种状态的步数int bfs();int main(){    int count = 0;    while(scanf("%d%d", &M, &N) != EOF && M && N){        if(count) printf("\n");    count++;        for(int i = 0; i < M; i++){            scanf("%s", maze[i]);            for(int j = 0; j < N; j++){                if(maze[i][j] == 'S'){s_x = i; s_y = j;}                else if(maze[i][j] == 'T'){e_x = i; e_y = j;}            }        }        s.x = s_x, s.y = s_y, s.color = 0, s.dir = 0;        e.x = e_x, e.y = e_y, e.color = 0;        memset(Step, -1, sizeof(Step));        Step[s.x][s.y][s.color][s.dir] = 0; q.push(s);//把初始状态放入到队列中,并将它的Step至为0        int ans = bfs();        if(ans > 0) printf("Case #%d\nminimum time = %d sec\n", count, ans);        else   printf("Case #%d\ndestination not reachable\n", count);    }    return 0;}int bfs(){    while(!q.empty()){//当队列不空时,则一直搜索        Cond temp = q.front(); q.pop();        if(temp.x == e.x && temp.y == e.y && temp.color == e.color){//如果找到了终点,则把队列清空并且返回到达终点的状态            while(!q.empty()) q.pop();            //printf("%d\n", Step[temp.x][temp.y][temp.color][temp.dir]);            return Step[temp.x][temp.y][temp.color][temp.dir];        }        for(int i = 0; i < 3; i++){//一共有3种状态可以转移            Cond next;            int j = temp.dir;            short xx, yy, cc, ddir;            xx = temp.x + dx[j][i]; yy = temp.y + dy[j][i]; cc = (temp.color+dc[i]) % 5; ddir = (temp.dir + dd[i] + 4) % 4;            //printf("%d\t%d\t%d\t%d\n", xx, yy, cc, ddir);            if(xx >= 0 && xx < M && yy >= 0 && yy < N && maze[xx][yy] != '#' && Step[xx][yy][cc][ddir]< 0){                next.x = xx; next.y = yy; next.color = cc; next.dir = ddir;                q.push(next); Step[xx][yy][cc][ddir] = Step[temp.x][temp.y][temp.color][temp.dir]+1;            }        }    }    return -1;}

0 0
原创粉丝点击