UVA 10047.The Monocycle(广搜)

来源:互联网 发布:k歌软件电视 编辑:程序博客网 时间:2024/05/01 00:54

此题较之Fire那道题而言相对简单,主要在于将不同颜色和不同方向的同一位置看作不同的点。

想通这个,就可以类比一般的广搜解决此题了。

Description

Download as PDF


  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 $M \times 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 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 ($1 \le M$$N \le 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 3S#T10 10#S.......##..#.##.###.##.##.##.#....##.###.##..#.##..#.##...#......##...##.##...#.###...#.#.....###T0 0

Sample Output 

Case #1destination not reachable Case #2minimum time = 49 sec



Miguel Revilla
2000-12-26

需要注意的是本题的dir方向数组,因为每一次只能向当前方向的左或右转,而且一开始对应的方向是北,所以压入队列的首个点应该方向和dir数组的[0][0]、[0][1]。

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <queue>using namespace std;int n, m;int vis[30][30][4][5];// x,y,dir,colorint dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};char map[30][30];int sx, sy, ex, ey;struct node {    int x, y, d, c, step;    node(int x, int y, int d, int c, int step) :    x(x), y(y), d(d), c(c), step(step) {}    node () {}};int BFS() {    memset(vis, 0, sizeof(vis));    queue<node> q;    q.push(node(sx, sy, 0, 0, 0));    vis[sx][sy][0][0] = 1;    while(!q.empty()) {        node a = q.front(); q.pop();        if(a.x == ex && a.y == ey && a.c == 0)            return a.step;        for(int i = 0; i < 4; i++) {            if(i == a.d) {                int x = a.x + dir[i][0], y = a.y + dir[i][1];                int c = (a.c + 1) % 5;                if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] != '#' && vis[x][y][i][c] == 0) {                    vis[x][y][i][c] = 1;                    q.push(node(x, y, i, c, a.step + 1));                }            }            else if((a.d + 1) % 4 == i || (a.d + 4 - 1) % 4 == i){                if(vis[a.x][a.y][i][a.c] == 0) {                    vis[a.x][a.y][i][a.c] = 1;                    q.push(node(a.x, a.y, i, a.c, a.step + 1));                }            }        }    }    return -1;}int main() {    int count = 1;    while(~scanf("%d%d", &n, &m)) {        if(n == 0 && m == 0) break;        for(int i = 0; i < n; i++) {            scanf("%s", map[i]);            for(int j = 0; j < m; j++) {                if(map[i][j] == 'S') {sx = i, sy = j;}                else if(map[i][j] == 'T') {ex = i, ey = j;}            }        }        int ans = BFS();        if(count != 1) printf("\n");        printf("Case #%d\n", count);        if(ans == -1) printf("destination not reachable\n");        else printf("minimum time = %d sec\n", ans);        count++;    }    return 0;}


0 0
原创粉丝点击