UVA10047_The Monocycle

这题。。。。有点奇葩,但是不难。

在矩形方阵里,某人可以往前走或者左拐右拐。都需要消耗一个单位时间。

问某人从一个点走向另一个点的最短时间,并且走过的路程是5的倍数。

由于n,m都小,直接f[n][m][direction][color],表示所有状态,bfs更新即可。

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int f[30][30][4][5];//position,direction,color
char s[30][30];
int n,m;

bool inside(int cx,int cy)
{
    return cx>0 && cx<=n && cy>0 && cy<=m;
}

void _init()
{
    for (int i=1; i<=n; i++)
        for (int j=1; j<=m; j++)
            for (int x=0; x<4; x++)
                for (int y=0; y<5; y++)
                    f[i][j][x][y]=99999999;
}

int _process()
{
    queue<int> qx,qy,qd,qc;
    for (int i=1; i<=n; i++)
    {
        scanf("%s",s[i]+1);
        for (int j=1; s[i][j]; j++)
            if (s[i][j]==‘S‘)
            {
                f[i][j][0][0]=0;
                qx.push(i),qy.push(j),qd.push(0),qc.push(0);
            }
    }
    while (!qx.empty())
    {
        int cx=qx.front(),cy=qy.front(),cd=qd.front(),cc=qc.front();
        qx.pop(),qy.pop(),qd.pop(),qc.pop();
        int tmp=(cd+3)%4;
        if (f[cx][cy][cd][cc]+1<f[cx][cy][tmp][cc])
        {
            f[cx][cy][tmp][cc]=f[cx][cy][cd][cc]+1;
            qx.push(cx),qy.push(cy),qd.push(tmp),qc.push(cc);
            /*
            cout<<" inside : "<<cx<<‘ ‘<<cy<<‘ ‘<<tmp<<‘ ‘;
            cout<<cc<<‘ ‘<<f[cx][cy][tmp][cc]<<endl;
            */
        }
        tmp=(cd+1)%4;
        if (f[cx][cy][cd][cc]+1<f[cx][cy][tmp][cc])
        {
            f[cx][cy][tmp][cc]=f[cx][cy][cd][cc]+1;
            qx.push(cx),qy.push(cy),qd.push(tmp),qc.push(cc);
            /*
            cout<<" inside : "<<cx<<‘ ‘<<cy<<‘ ‘<<tmp;
            cout<<‘ ‘<<cc<<‘ ‘<<f[cx][cy][tmp][cc]<<endl;
            */
        }
        if (!inside(cx+dx[cd],cy+dy[cd]) || s[cx+dx[cd]][cy+dy[cd]]==‘#‘) continue;
        if (f[cx][cy][cd][cc]+1<f[cx+dx[cd]][cy+dy[cd]][cd][(cc+1)%5])
        {
            f[cx+dx[cd]][cy+dy[cd]][cd][(cc+1)%5]=f[cx][cy][cd][cc]+1;
            if (s[cx+dx[cd]][cy+dy[cd]]==‘T‘ && (cc+1)%5==0) return f[cx+dx[cd]][cy+dy[cd]][cd][0];
            qx.push(cx+dx[cd]),qy.push(cy+dy[cd]),qd.push(cd),qc.push((cc+1)%5);
            /*
            cout<<" inside : "<<cx+dx[cd]<<‘ ‘<<cy+dy[cd]<<‘ ‘<<cd;
            cout<<‘ ‘<<(cc+1)%5<<‘ ‘<<f[cx+dx[cd]][cy+dy[cd]][cd][(cc+1)%5]<<endl;
            */
        }
    }
    return -1;
}

int main()
{
    //freopen("data.in","rb",stdin);
    int cas=0;
    while (scanf("%d%d",&n,&m) && (n|m))
    {
        _init();
        int ans=_process();
        if (cas) printf("\n");
        printf("Case #%d\n",++cas);
        if (ans==-1) puts("destination not reachable");
            else printf("minimum time = %d sec\n",ans);
    }
    return 0;
}

  

UVA10047_The Monocycle

时间: 2024-11-08 07:19:53

UVA10047_The Monocycle的相关文章

uva 10047 - The Monocycle bfs

题目链接 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

UVA 10047 - The Monocycle

题目如下:  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 differentcolors as shown in the figure: The colored segments make equal an

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

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

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 10047 uva live 2035 The Monocycle bfs

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

UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接 题意:从起点到终点,每秒可以选择前进.向左.向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间. 思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维. 水题. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&g

UVa10047 The Monocycle

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19491 (以上摘自http://blog.csdn.net/shuangde800/article/details/7686003) [思路] BFS. 以节点位置(x,y)方向dir底面的颜色c时间d为状态宽搜,每次只有左转.右转.向前走三个拓展. 需要注意的有初始状态为(sx,sy,0,0),update有效简约了代码. [代码] 1 #include<cstdi

UVa (BFS) The Monocycle

题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方向朝dir与地面接触的扇形的颜色为color,这个状态是否到达过. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn =