24h購物| | PChome| 登入
2013-04-03 11:57:33| 人氣604| 回應0 | 上一篇 | 下一篇

[UVA][bfs] 589 - Pushing Boxes

推薦 0 收藏 0 轉貼0 訂閱站台


  Pushing Boxes 

Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not be filled with rock. You can move north, south, east or west one cell at a step. These moves are called walks.

One of the empty cells contains a box which can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. Such a move is called a push. The box cannot be moved in any other way than by pushing, which means that if you push it into a corner you can never get it out of the corner again.

One of the empty cells is marked as the target cell. Your job is to bring the box to the target cell by a sequence of walks and pushes. As the box is very heavy, you would like to minimize the number of pushes. Can you write a program that will work out the best such sequence?

Input 

The input file contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both $leŸ 20$) representing the number of rows and columns of the maze.

Following this are r lines each containing c characters. Each character describes one cell of the maze. A cell full of rock is indicated by a `#' and an empty cell is represented by a `.'. Your starting position is symbolized by `S', the starting position of the box by `B' and the target cell by `T'.

Input is terminated by two zeroes for r and c.

Output 

For each maze in the input, first print the number of the maze, as shown in the sample output. Then, if it is impossible to bring the box to the target cell, print ``Impossible.''.

Otherwise, output a sequence that minimizes the number of pushes. If there is more than one such sequence, choose the one that minimizes the number of total moves (walks and pushes). If there is still more than one such sequence, any one is acceptable.

Print the sequence as a string of the characters N, S, E, W, n, s, e and w where uppercase letters stand for pushes, lowercase letters stand for walks and the different letters stand for the directions north, south, east and west.

Output a single blank line after each test case.

Sample Input 

1 7
SB....T
1 7
SB..#.T
7 11
###########
#T##......#
#.#.#..####
#....B....#
#.######..#
#.....S...#
###########
8 4
....
.##.
.#..
.#..
.#.B
.##S
....
###T
0 0

Sample Output 

Maze #1
EEEEE

Maze #2
Impossible.

Maze #3
eennwwWWWWeeeeeesswwwwwwwnNN

Maze #4
swwwnnnnnneeesssSSS



Miguel A. Revilla
1998-03-10

bfs 狀態記錄 step[sx][sy][bx][by] 人跟箱子的位置.

由於只要求 push 次數最少, 而不是總步數最少, 因此改一下權重,
push*10000+walks, 然後使用 priority_queue
每次找最少的出來拓展。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;
int used[25][25][25][25] = {};
int step[25][25][25][25] = {};
struct Node {
    int x, y, a, b, v;
    Node(int a1, int a2, int a3, int a4, int a5) {
        x = a1, y = a2;
        a = a3, b = a4;
        v = a5;
    }
};
struct cmp {
    bool operator() (Node a, Node b) {
        return a.v > b.v;
    }
};
void print(int x, int y, int a, int b) {
    //printf("%d %d %d %d %d\n", x, y, a, b, used[x][y][a][b]);
    if(used[x][y][a][b] == 1)
        return;
    static int dirx[4] = {1,0,0,-1};
    static int diry[4] = {0,-1,1,0}; // NEWS/news
    static char chd[4] = {'N','E','W','S'};
    int i = 0;
    for(i = 0; i < 4; i++) {
        if(step[x][y][a][b] == chd[i]) {
            print(x+dirx[i], y+diry[i], a+dirx[i], b+diry[i]);
        }
        if(step[x][y][a][b] == chd[i]+32) {
            print(x+dirx[i], y+diry[i], a, b);
        }
    }
    putchar(step[x][y][a][b]);
}
int main() {
    int n, m, i, j, k;
    int cases = 0;
    char g[25][25];
    while(scanf("%d %d", &n, &m) == 2) {
        if(n == 0)
            break;
        printf("Maze #%d\n", ++cases);
        for(i = 0; i < n; i++)
            scanf("%s", g[i]);
        memset(used, 0, sizeof(used));
        int bx, by, sx, sy;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                if(g[i][j] == 'B')
                    bx = i, by = j;
                if(g[i][j] == 'S')
                    sx = i, sy = j;
            }
        }
        used[sx][sy][bx][by] = 1;
        priority_queue<Node, vector<Node>, cmp> Q;
        Q.push(Node(sx, sy, bx, by, 1));
        Node tn(0,0,0,0,0);
        int tx, ty;
        int dirx[4] = {-1,0,0,1};
        int diry[4] = {0,1,-1,0}; // NEWS/news
        char chd[4] = {'N','E','W','S'};
        int sol = 0;
        while(!Q.empty()) {
            tn = Q.top(), Q.pop();
            //printf("%d %d %d %d %c\n", tn.x, tn.y, tn.a, tn.b, step[tn.x][tn.y][tn.a][tn.b]);
            //printf("%d\n", step[tn.x][tn.y][tn.a][tn.b]);
            if(g[tn.a][tn.b] == 'T') {
                print(tn.x, tn.y, tn.a, tn.b);
                puts("");
                sol = 1;
                break;
            }
            for(i = 0; i < 4; i++) {// NEWS
                if(tn.a == tn.x+dirx[i] && tn.b == tn.y+diry[i]) {
                    tx = tn.a+dirx[i], ty = tn.b+diry[i];
                    if(tx < 0 || ty < 0 || tx >= n || ty >= m)
                        continue;
                    if(g[tx][ty] == '#')    continue;
                    int *p = &used[tn.x+dirx[i]][tn.y+diry[i]][tn.a+dirx[i]][tn.b+diry[i]];
                    if(*p <= used[tn.x][tn.y][tn.a][tn.b]+100000 && *p)  continue;
                    *p = used[tn.x][tn.y][tn.a][tn.b]+100000;
                    step[tn.x+dirx[i]][tn.y+diry[i]][tn.a+dirx[i]][tn.b+diry[i]] = chd[i];
                    Q.push(Node(tn.x+dirx[i], tn.y+diry[i], tn.a+dirx[i], tn.b+diry[i], used[tn.x][tn.y][tn.a][tn.b]+100000));
                }
            }
            for(i = 0; i < 4; i++) {//news
                if(tn.a == tn.x+dirx[i] && tn.b == tn.y+diry[i])
                    continue;
                tx = tn.x+dirx[i], ty = tn.y+diry[i];
                if(tx < 0 || ty < 0 || tx >= n || ty >= m)
                    continue;
                if(g[tx][ty] == '#')    continue;
                int *p = &used[tn.x+dirx[i]][tn.y+diry[i]][tn.a][tn.b];
                if(*p <= used[tn.x][tn.y][tn.a][tn.b]+1 && *p)  continue;
                *p = used[tn.x][tn.y][tn.a][tn.b]+1;
                step[tn.x+dirx[i]][tn.y+diry[i]][tn.a][tn.b] = chd[i]+32;
                Q.push(Node(tn.x+dirx[i], tn.y+diry[i], tn.a, tn.b, used[tn.x][tn.y][tn.a][tn.b]+1));
            }
        }
        if(!sol)
            puts("Impossible.");
        puts("");
    }
    return 0;
}

台長: Morris
人氣(604) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][旋轉、縮放] 10206 - Stars
此分類上一篇:[UVA][SSSP] 523 - Minimum Transport Cost

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文