#POJ1475#Pushing Boxes(A*_搜索)

9 篇文章 0 订阅

Pushing Boxes
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 6117 Accepted: 2115 Special Judge

Description

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 contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both <= 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


题意:

给出多个地图,求把箱子推到T的最少推的次数,若有多个方案,输出走路最少的,输出路径,大写字母表示推箱子,小写字母表示人走。


A*搜索,H值是题目天然给出的推箱子数,每次搜索,注意更新,然后其实不难写,就是看着比较宽。

注意记录路径,倒着装回去再输出。


Code:

Status Accepted
Time 250ms
Memory 9980kB
Length 3868
Lang G++
Submitted
Shared

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int Max = 20;

struct node{
    int x, y;
    node(){}
    node(int a, int b){x = a, y = b;}
    bool operator == (const node & X)const{
        return x == X.x && y == X.y;
    }
}S, B, T;
struct A{
    int pu, wl;
    node p, b;
    A(){}
    A(int a, int x, node c, node d){pu = a, wl = x, p = c, b = d;}
    bool operator > (const A & X)const{
        if(pu == X.pu)  return wl > X.wl;
        return pu > X.pu;
    }
};

int N, M;
char map[Max + 5][Max + 5], rt[1000];
int dd[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int H[Max + 5][Max + 5][Max + 5][Max + 5][2], fa[Max + 5][Max + 5][Max + 5][Max + 5][4];

bool Check_Move(node t){
    if(t.x < 1 || t.y < 1 || t.x > N || t.y > M)    return 0;
    return map[t.x][t.y] != '#';
}
bool Check_now_better(A t){
    if(H[t.p.x][t.p.y][t.b.x][t.b.y][0] == t.pu)
        return t.wl < H[t.p.x][t.p.y][t.b.x][t.b.y][1];
    return t.pu < H[t.p.x][t.p.y][t.b.x][t.b.y][0];
}

void Print(node p, node b, int mvs){
    int x1, y1, x2, y2;
    char tmp;
    rt[mvs + 1] = 0;
    for( ; mvs; -- mvs){
        x1 = fa[p.x][p.y][b.x][b.y][0],
        y1 = fa[p.x][p.y][b.x][b.y][1],
        x2 = fa[p.x][p.y][b.x][b.y][2],
        y2 = fa[p.x][p.y][b.x][b.y][3];
        if(x2 != b.x || y2 != b.y)  tmp = 'A';
        else tmp = 'a';
        if(p.x - x1 == 1)   rt[mvs] = 'S' - 'A' + tmp;
        if(p.y - y1 == 1)   rt[mvs] = 'E' - 'A' + tmp;
        if(p.x - x1 == -1)  rt[mvs] = 'N' - 'A' + tmp;
        if(p.y - y1 == -1)  rt[mvs] = 'W' - 'A' + tmp;
        p.x = x1, p.y = y1, b.x = x2, b.y = y2;
    }
    printf("%s\n", rt + 1);
}

void Astar(){
    memset(H, 0x3f, sizeof H );
    memset(fa, 0x3f, sizeof fa );
    priority_queue<A, vector<A>, greater<A> >Q;
    A t = A(0, 0, S, B), newin;
    Q.push(t);
    while(! Q.empty()){
        t = Q.top();    Q.pop();
        if(t.b == T){   Print(t.p, t.b, t.pu + t.wl);    return ;}
        for(int i = 0; i < 4; ++ i){
            newin.p.x = t.p.x + dd[i][0];
            newin.p.y = t.p.y + dd[i][1];
            if(! Check_Move(newin.p))   continue;
            if(newin.p == t.b){//推箱子走
                newin.b.x = t.b.x + dd[i][0];
                newin.b.y = t.b.y + dd[i][1];
                if(! Check_Move(newin.b))   continue;
                newin.pu = t.pu + 1;
                newin.wl = t.wl;
            }
            else newin.b = t.b, newin.wl = t.wl + 1, newin.pu = t.pu;//人走
            if(Check_now_better(newin)){
                fa[newin.p.x][newin.p.y][newin.b.x][newin.b.y][0] = t.p.x;
                fa[newin.p.x][newin.p.y][newin.b.x][newin.b.y][1] = t.p.y;
                fa[newin.p.x][newin.p.y][newin.b.x][newin.b.y][2] = t.b.x;
                fa[newin.p.x][newin.p.y][newin.b.x][newin.b.y][3] = t.b.y;
                H[newin.p.x][newin.p.y][newin.b.x][newin.b.y][0] = newin.pu;
                H[newin.p.x][newin.p.y][newin.b.x][newin.b.y][1] = newin.wl;
                Q.push(newin);
            }
        }
    }
    puts("Impossible.");
}

int main(){
    int cas = 0;
    while(~ scanf("%d%d", &N, &M) && N){
        for(int i = 1; i <= N; ++ i)
            scanf("%s", map[i] + 1);
        for(int i = 1; i <= N; ++ i)
            for(int j = 1; j <= M; ++ j){
                if(map[i][j] == 'S')    S = node(i, j);
                if(map[i][j] == 'B')    B = node(i, j);
                if(map[i][j] == 'T')    T = node(i, j);
            }
        printf("Maze #%d\n", ++ cas);
        Astar();
        putchar(10);
    }
    return 0;
}
/*
1 7
SB....T
1 7
SB..#.T
7 11
###########
#T##......#
#.#.#..####
#....B....#
#.######..#
#.....S...#
###########
8 4
....
.##.
.#..
.#..
.#.B
.##S
....
###T
8 9
#########
#......T#
#.S.....#
##B######
#.......#
#.......#
#.......#
#########
0 0

SSesswNNNNwnEEEEE
*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值