码迷,mamicode.com
首页 > 其他好文 > 详细

The Monocycle(BFS)

时间:2014-07-22 23:08:53      阅读:444      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   width   

The Monocycle

Time Limit: 3000MS
64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

mamicode.com,码迷

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:

mamicode.com,码迷

The colored segments make equal angles (72o) at the center. A monocyclist rides this cycle on an mamicode.com,码迷 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.

mamicode.com,码迷

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 (mamicode.com,码迷, mamicode.com,码迷) 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 3
S#T
10 10
#S.......#
#..#.##.##
#.##.##.##
.#....##.#
##.##..#.#
#..#.##...
#......##.
..##.##...
#.###...#.
#.....###T
0 0

Sample Output

Case #1
destination not reachable
 
Case #2
minimum time = 49 sec
 
题目大意:给一个迷宫,你骑着独轮车在迷宫行进,每前进一格轮子与地面接触的颜色都会变化,总共有5种
颜色依次循环,在一个格子中,可以沿前进方向走一步,左转和右转,每个动作耗时1秒,起始向北,给定
入口和出口,要求到出口时,轮胎与地面接触的颜色与初始时相同,有路径则求最短路径,否则输出不可能
 
如果没有颜色限制,那么就是简单的最短路,直接BFS,但是这道题,到每个格子的接触地面的扇形颜色、朝向都
可能不同,那我们就可以把一个格子看成多个点,对于同一个格子与地面接触的扇形颜色(5种)、朝向(4个)不同
看成不同的点,那么题目就可以转化为给定起点(朝向,颜色)和终点,求最短路。
 
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
#define next(a) ((a+1)%5)//下一种颜色
const int N=26;
char g[N][N];
bool vis[N][N][4][5];//值为1表示某点是否遍历过,点的要素x,y,direction,color;
int n,m,cas=1;
int dx[4]={-1,0,1,0};//4个方向,本人0代表方向想上故dx[0]=-1,dy[0]=0,其它类推。。表示因为方向问题debug好久
int dy[4]={0,1,0,-1};

struct node{
    int x,y,dir,col,step;
    node() {}
    node(int a,int b,int c,int d,int e):x(a),y(b),dir(c),col(d),step(e) {}
};

bool is_ok(int x,int y){
     return x>=0&&x<n&&y>=0&&y<m&&g[x][y]!=‘#‘;
}

void bfs()
{
    queue<node >q;
    memset(vis,0,sizeof(vis));
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
            if(g[i][j]==‘S‘) q.push(node(i,j,0,0,0)),vis[i][j][0][0]=1;
        }
    while(!q.empty())
    {
        node e=q.front(); q.pop();
        if(g[e.x][e.y]==‘T‘&&e.col==0){
            printf("minimum time = %d sec\n",e.step);
            return;
        }
        //下面分别是向左右转,前进,至于为什么不向后转(向左转两次),
        //因为BFS是按时间递增(1s)来遍历,向后转耗时两秒,遍历出来不一定是最优解
        int x=e.x,y=e.y,c=e.col;
        int d=(e.dir+1)%4;
        if(!vis[x][y][d][c])//向右转
        {
            vis[x][y][d][c]=1;
            q.push(node(x,y,d,c,e.step+1));
        }
        d=(e.dir+3)%4;
        if(!vis[x][y][d][c])//向左转
        {
            vis[x][y][d][c]=1;
            q.push(node(x,y,d,c,e.step+1));
        }
        d=e.dir;
        x+=dx[d],y+=dy[d],c=next(c);
        if(is_ok(x,y)&&!vis[x][y][d][c]){//前进
            q.push(node(x,y,d,c,e.step+1));
            vis[x][y][d][c]=1;
        }
    }
    puts("destination not reachable");
}

int main()
{
//    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)>0&&(n|m))
    {
        if(cas>1) printf("\n");
        for(int i=0; i<n; i++) scanf("%s",g[i]);
        printf("Case #%d\n",cas++);
        bfs();
    }
    return 0;
}

The Monocycle(BFS),码迷,mamicode.com

The Monocycle(BFS)

标签:des   style   http   color   os   width   

原文地址:http://www.cnblogs.com/zyx1314/p/3700378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!