UVA The Monocycle(BFS 4种状态)

  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 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 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


     题意:一个小车停在S点,现在它要到T点,走的规则是(上 下 左 右)

四个方向其中每当小车进行转方向的时候会消耗时间,每转90度会消耗1秒时间

同理180度时会消耗2秒时间,另外小车的轮子上有五种颜色(蓝色,红色,黑色,

绿色,白色),在前进的时候每1秒轮子会转72度(进行转弯的时候不算前进),

在S点的时候轮子底部处于蓝色地方,小车面向北方(上北下南,左西右东),要

求小车到达T点时,轮子底部的颜色还是蓝色,方向不要求。求符合条件的最小时间。

    注意:小车每当走过一个方格,它的轮子底部的颜色和现在处于的方向都会被

          标记,下次这种颜色和方向和颜色就不可再走这个方格。要注意这一点。

    思路:因为除了坐标还有颜色和方向两种状态,所以标记数组应该设成四维的。

          然后按照BFS的思路就可以了。


代码:


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>

using namespace std;

struct node
{
    int x;
    int y;
    int z;
    int time;
    int color;
};

bool operator < (const node &a, const node &b)
{
    return a.time > b.time;
}

int n,m;
char map[31][31];
int v[26][26][10][10];
int Color[] = {0,1,2,3,4};
int jx[] = {-1,0,1,0};
int jy[] = {0,1,0,-1};

void BFS(int pi_1,int pj_1,int pi_2,int pj_2)
{
    memset(v,0,sizeof(v));
    priority_queue<node>q;
    node t,f;
    int pz;
    t.x = pi_1;
    t.y = pj_1;
    t.z = 0;
    t.color = 0;
    t.time = 0;
    q.push(t);
    v[t.x][t.y][t.color][t.z] = 1;
    while(!q.empty())
    {
        t = q.top();
        //printf("t.x = %d   t.y = %d   t.time = %d    t.color = %d\n",t.x,t.y,t.time,t.color);
        if(t.x == pi_2 && t.y == pj_2 && t.color == 0)
        {
            printf("minimum time = %d sec\n",t.time);
            return ;
        }
        q.pop();
        for(int i=0; i<4; i++)
        {
            f.x = t.x + jx[i];
            f.y = t.y + jy[i];
            if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && map[f.x][f.y]!='#')
            {
                if(t.z == i)
                {
                    f.time = t.time + 1;
                    f.z = i;
                    f.color = (t.color + 1)%5;
                }
                else
                {
                    f.x = t.x;
                    f.y = t.y;
                    pz = fabs(t.z - i);
                    if(pz % 2 == 0)
                    {
                        f.time = t.time + 2;
                    }
                    else
                    {
                        f.time = t.time + 1;
                    }
                    f.color = t.color;
                    f.z = i;
                }
                if(v[f.x][f.y][f.color][f.z] == 0)
                {
                    q.push(f);
                    v[f.x][f.y][f.color][f.z] = 1;
                }
            }
            
        }
    }
    printf("destination not reachable\n");
}

int main()
{
    int kk = 0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n == 0 && m == 0)
        {
            break;
        }
        int pi_1,pi_2,pj_1,pj_2;
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(int j=0; j<m; j++)
            {
                if(map[i][j] == 'S')
                {
                    pi_1 = i;
                    pj_1 = j;
                }
                else if(map[i][j] == 'T')
                {
                    pi_2 = i;
                    pj_2 = j;
                }
            }
        }
        if(kk>0)
        {
            printf("\n");
        }
        printf("Case #%d\n",++kk);
        BFS(pi_1,pj_1,pi_2,pj_2);
    }
    return 0;
}



Miguel Revilla
2000-12-26

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值