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

时间: 2024-10-02 09:14:52

UVA The Monocycle(BFS 4种状态)的相关文章

UVA 10047-The Monocycle(队列bfs+保存4种状态)

题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间到达终点,如果无法到达,输出  destination not reachable,起点状态是朝北,着地颜色是绿色,到达终点的条件是着地颜色是绿色,方向任意. 解析:bfs搜一遍,但要保存4种状态,分别是坐标x,y,方向和颜色.每次选择有3种,1.向前进,2.左转,3.右转. 代码如下: #incl

uva 10047 uva live 2035 The Monocycle bfs

// uva 10047 uva live 2035 bfs // 求最短的嘛,肯定先尝试bfs啦 // 确定状态,首先状态里面得有坐标x,y // 还得有朝向,还得有颜色值 // // 这样就是一个状态里面有着三种属性 // 每个状态都只要经历一次,再经历是没有任何意义的 // 用一个que的思维数组记录就行了. // 按照方向爆搜,我先用f[i][j]记录的就是到 // 这一点的最小距离,但是怎么都过不了样例 // 突然明白了,如果只是这样记录最短距离,是不行的 // 因为每次从队列中取出的

The Monocycle(BFS)

The Monocycle Time Limit: 3000MS64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description 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 w

uva 11825 ,Hacker&#39;s Crackdown 状态压缩 dp

// uva 11825 Hacker's Crackdown // // 题目意思看了很久才看懂,有n台计算机,有n种服务,每台计算机上运行所有 // 的服务,并且其中有的计算机与某些计算机相互邻接,对于每台计算机, // 你可以选择一项服务,停止这项服务,则与它邻接的计算机的该服务也停止了 // 你的目的是让经量多的服务完全瘫痪 // // 换而言之,这个问题就是在n个集合中(p[1]....p[n])分成尽量多的组数,使得每组 // 的并集等于全集(即所有的n台电脑都停止)... // /

java线程五种状态

java线程五种状态: 创建 -> 就绪 -> 运行 -> 销毁 创建 -> 就绪 -> 运行 -> 等待(缺少资源) -> 销毁 下图:各种状态转换

Hibernate的三种状态

 Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached).处于持久态的对象也称为PO(Persistence Object),瞬时对象和脱管对象也称为VO(Value Object).  瞬时态 Transient 由new命令开辟内存空间的java对象, eg. Person person = new Person("xxx", "xx"); 如果没有变量对该对象进行引用,它将被jav

深入hibernate的三种状态(转)

学过hibernate的人都可能都知道hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),大家伙也许也知道这三者之间的区别,比如瞬时状态就是刚new出来一个对象,还没有被保存到数据库中,持久化状态就是已经被保存到数据库中,离线状态就是数据库中有,但是session中不存在该对象.但是大家又是否对hibernate的session的那几个特殊方法一清二楚呢?或者说大家是否能够一眼就快速看出一个测试用例在反复的调用sess

深入hibernate的三种状态

学过hibernate的人都可能都知道hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),大家伙也许也知道这三者之间的区别,比如瞬时状态就是刚new出来一个对象,还没有被保存到数据库中,持久化状态就是已经被保存到数据库中,离线状态就是数据库中有,但是session中不存在该对象.但是大家又是否对hibernate的session的那几个特殊方法一清二楚呢?或者说大家是否能够一眼就快速看出一个测试用例在反复的调用sess

简单理解Hibernate三种状态的概念及互相转化

本文描述了Hibernate三种状态的概念及互相转化.Java对象的生命周期中有三种状态,而且互相转化.它们分别是临时状态,持久化状态,以及游离状态. AD:WOT2015 互联网运维与开发者大会 热销抢票 在Hibernate中有三种状态,对它的深入理解,才能更好的理解hibernate的运行机理,刚开始不太注意这些概念,后来发现它是重要的.对于理解hibernate,JVM和sql的关系有更好的理解.对于需要持久化的JAVA对象,在它的生命周期中有三种状态,而且互相转化. Hibernate