UVA10047- The Monocycle(BFS)

题目链接

题意:一自行车的轮子被分成5个扇区,涂了5种不同颜色。自行车每1秒要么骑到下一个格子,要么左转或者右转90。。一开始自行车面向北,颜色为绿,到达目标格时,必须触底颜色为绿,但朝向无限制。求到达目标格的最短时间。

思路:判重数组多加两维,分别为朝向和颜色,之后就可以用BFS求最少时间了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

const int MAXN = 30;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct node{
    node (int xx, int yy, int dd, int cc, int tt) {
        x = xx;
        y = yy;
        d = dd;
        c = cc;
        t = tt;
    }
    int x, y, d, c, t;
};

char g[MAXN][MAXN];

int m, n, sx, sy, ex, ey;
int vis[MAXN][MAXN][6][6];

int bfs() {
    queue<node> q;
    while (!q.empty()) q.pop();
    node s(sx, sy, 0, 0, 0);
    q.push(s);
    memset(vis, 0, sizeof(vis));
    vis[sx][sy][0][0] = 1;
    while (!q.empty()) {
        node u = q.front();
        q.pop(); 

        if (u.x == ex && u.y == ey && u.c == 0)
            return u.t;

        for (int i = 0; i < 4; i++) {
            node v = u;
            if (i == u.d) {
                v.x = u.x + dx[i];
                v.y = u.y + dy[i];
                v.c = (u.c + 1) % 5;
                v.t = u.t + 1;
                if (v.x < 0 || v.x >= n || v.y < 0 || v.y >= m || g[v.x][v.y] == '#') continue;
                if (!vis[v.x][v.y][v.d][v.c]) {
                    vis[v.x][v.y][v.d][v.c] = 1;
                    q.push(v);
                }
            }
            else if ((u.d + 1) % 4 == i || (u.d + 3) % 4 == i){
                if (!vis[v.x][v.y][i][v.c]) {
                    vis[v.x][v.y][i][v.c] = 1;
                    v.d = i;
                    v.t = u.t + 1;
                    q.push(v);
                }
            }
        }
    }
    return -1;
}

int main() {
    int t = 1, flag = 0;
    while (scanf("%d%d", &n, &m) && n && m) {
        for (int i = 0; i < n; i++)
            scanf("%s", g[i]);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (g[i][j] == 'S') {
                    sx = i;
                    sy = j;
                }
                if (g[i][j] == 'T') {
                    ex = i;
                    ey = j;
                }
            }
        }
        if (flag)
            printf("\n");
        flag = 1;

        printf("Case #%d\n", t++);
        int ans = bfs();
        if (ans == -1)
            printf("destination not reachable\n");
        else
            printf("minimum time = %d sec\n", ans);
    }
    return 0;
}

时间: 2024-08-28 07:13:25

UVA10047- The Monocycle(BFS)的相关文章

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

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

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

【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

UVa10047 BFS

题意:一自行车的轮子被分成5个扇区,涂了5种不同颜色.自行车每1秒要么骑到下一个格子,要么左转或者右转90..一开始自行车面向北,颜色为绿,到达目标格时,必须触底颜色为绿,但朝向无限制.求到达目标格的最短时间. 思路:可以朝3个方向搜索,多了一种颜色状态,每个结点有四个值需要保存,坐标x,坐标y,朝向,底面颜色.每个结点只可以执行一次,不然会出现死循环.所以需要vis数组标记. 代码: #include<cstdio> #include<cstring> #include<q

UVA 10047 - The Monocycle(BFS)

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

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 =