UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接

题意:从起点到终点,每秒可以选择前进、向左、向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间。

思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维。 水题。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 5;
const int INF = 1000000000;
const int maxn = 50;
int t,n,m,d[maxn][maxn][6][4],kase=0;
int dir[] = {0,1,2,3}; // 东南西北
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct node {
    int x, y, col, dir;
    node(int x=0, int y=0, int col=0, int dir=0):x(x),y(y),col(col),dir(dir) {}
}S,T;
char s[maxn][maxn];
int BFS() {
    queue<node> q;
    memset(d, -1, sizeof(d));
    d[S.x][S.y][S.col][S.dir] = 0;
    q.push(S);
    while(!q.empty()) {
        node u = q.front(); q.pop();
        if(u.x == T.x && u.y == T.y && u.col == 0) return d[u.x][u.y][u.col][u.dir];
        node v = u;
        v.x = v.x + dx[v.dir];
        v.y = v.y + dy[v.dir];
        v.col = (v.col + 1) % mod;
        if(v.x >= 1 && v.x <= n && v.y >= 1 && v.y <= m && d[v.x][v.y][v.col][v.dir] == -1 && s[v.x][v.y] != '#') {
            d[v.x][v.y][v.col][v.dir] = d[u.x][u.y][u.col][u.dir] + 1;
            q.push(v);
        }
        v = u;
        v.dir = (v.dir - 1 + 4) % 4;
        if(v.x >= 1 && v.x <= n && v.y >= 1 && v.y <= m && d[v.x][v.y][v.col][v.dir] == -1 && s[v.x][v.y] != '#') {
            d[v.x][v.y][v.col][v.dir] = d[u.x][u.y][u.col][u.dir] + 1;
            q.push(v);
        }
        v = u;
        v.dir = (v.dir + 1) % 4;
        if(v.x >= 1 && v.x <= n && v.y >= 1 && v.y <= m && d[v.x][v.y][v.col][v.dir] == -1 && s[v.x][v.y] != '#') {
            d[v.x][v.y][v.col][v.dir] = d[u.x][u.y][u.col][u.dir] + 1;
            q.push(v);
        }
    }
    return -1;
}
int main() {
    while(~scanf("%d%d",&n,&m)) {
        if(n == 0 && m == 0) return 0;
        if(kase) printf("\n");
        for(int i=1;i<=n;i++) {
            scanf("%s",s[i]+1);
        }
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                if(s[i][j] == 'S') S = node(i, j, 0, 3);
                else if(s[i][j] == 'T') T = node(i, j, 0, 0);
            }
        }
        int ans = BFS();
        printf("Case #%d\n",++kase);
        if(ans == -1) printf("destination not reachable\n");
        else printf("minimum time = %d sec\n",ans);
    }
    return 0;
}

时间: 2024-10-12 00:59:27

UVA 10047 - The Monocycle(BFS)的相关文章

UVA - 10047 The Monocycle (BFS)

题目大意:有一个n*m的网格,网格上面有的地方有障碍物 现在有一个人,骑着独轮车,要求从一个地方到达另一个地方,骑独轮车时,只能直走,或者左拐,右拐,不能向后走 独轮车的轮子被分成了5部分,每部分都有对应的颜色,刚开始时是绿色向下,当经过一个格子时,颜色就会变换 问从起点出发到终点,到终点时独轮车的绿色颜色向下,需要多久 解题思路:暴力BFS #include <cstdio> #include <cstring> #include <algorithm> #inclu

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

uva 10651 Pebble Solitaire (BFS)

uva 10651 Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as

uva 532 Dungeon Master(BFS)

uva 532 Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. Y

uva 11624 Fire!(BFS)

Fire! Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a f

UVA 816 - Abbott&#39;s Revenge(BFS)

UVA 816 - Abbott's Revenge 题目链接 题意:一个迷宫,每个点限制了从哪一方向来的,只能往左右前走,然后问起点到终点的最短路径 思路:BFS,每个点拆成4个方向的点,对应能走的方向建图跑一下bfs即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace

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

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤