UVA10047 UVALive2035 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 monocyclist rides this cycle on an M × 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 segme nt 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 90 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 ≤ M, N ≤ 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

Regionals 2000 >> Asia - Shanghai

问题链接UVA10047 UVALive2035 The Monocycle
问题简述:(略)
问题分析
    从起点到终点,每秒可以选择前进、左转和右转,每前进一格轮子转到下一个颜色,共有5种颜色,开始的时候绿色接触地面并且朝北,要求最后也绿色接触地面。计算能否到达目标点?能的话最短时间为多少?
    这个题需要考虑一般BFS问题的搜索过程,同时也需要考虑另外2个因素,即颜色和方向。用数组states[][][][]来记忆状态是否出现过,如果出现过则不再搜索。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10047 UVALive2035 The Monocycle */

#include <bits/stdc++.h>

using namespace std;

const int DN = 4;       // 4个方向
int drow[] = {0, 1, 0, -1};     // 东南西北
int dcol[] = {1, 0, -1, 0};

const int CN = 5;       // 颜色数量
const int N = 25;
char s[N][N + 1];
struct Node {
    int row, col, color, dir;
    Node(int x=0, int y=0, int col=0, int dir=0):row(x),col(y),color(col),dir(dir) {}
} start, target;
int state[N][N][CN][DN];
int m, n, caseno = 0;

int bfs()
{
    memset(state, -1, sizeof(state));

    queue<Node> q;

    state[start.row][start.col][start.color][start.dir] = 0;
    q.push(start);

    while(!q.empty()) {
        Node u = q.front();
        q.pop();
        if(u.row == target.row && u.col == target.col && u.color == 0)
            return state[u.row][u.col][u.color][u.dir];

        Node v = u;
        v.row = v.row + drow[v.dir];
        v.col = v.col + dcol[v.dir];
        if(v.row >= 0 && v.row < m && v.col >= 0 && v.col < n) {
            v.color = (v.color + 1) % CN;
            if(state[v.row][v.col][v.color][v.dir] == -1 && s[v.row][v.col] != '#') {
                state[v.row][v.col][v.color][v.dir] = state[u.row][u.col][u.color][u.dir] + 1;
                q.push(v);
            }
        }

        v = u;
        v.dir = (v.dir - 1 + DN) % DN;
        if(state[v.row][v.col][v.color][v.dir] == -1 && s[v.row][v.col] != '#') {
            state[v.row][v.col][v.color][v.dir] = state[u.row][u.col][u.color][u.dir] + 1;
            q.push(v);
        }

        v = u;
        v.dir = (v.dir + 1) % DN;
        if(state[v.row][v.col][v.color][v.dir] == -1 && s[v.row][v.col] != '#') {
            state[v.row][v.col][v.color][v.dir] = state[u.row][u.col][u.color][u.dir] + 1;
            q.push(v);
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d%d", &m, &n) && (m || n)) {
        for(int i = 0; i < m; i++)
            scanf("%s", s[i]);

        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(s[i][j] == 'S')
                    start = Node(i, j, 0, 3);
                else if(s[i][j] == 'T')
                    target = Node(i, j, 0, 0);
            }
        }

        int ans = bfs();

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

posted on 2019-03-10 20:53  新海岛Blog  阅读(137)  评论(0编辑  收藏  举报

导航

// ... runAll: function() { this.resetPreCode(); hljs.initHighlightingOnLoad(); // 重新渲染,添加语法高亮 hljs.initLineNumbersOnLoad(); // 为代码加上行号 } // ...