[POJ1475]Pushing Boxes

Time Limit: 2000MS
Memory Limit: 131072K
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

题意:
人在S点,箱子在B点,目标点在T点。
求用最少的推箱子次数将箱子从B处推到T处的方案。
推箱子的动作用大写字母,否则用小写字母。
每个答案后有一个多余空行。

题解:
用优先队列,让推箱子次数少的状态先出队。
然后搜索时判断当前人的动作是推箱子还是不推箱子。
细节题。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#define LiangJiaJun main
#define pa pair<int,int>
using namespace std;
int r,c,from[160004];
struct data{int bx,by,px,py,pc,bc,pos;};
bool operator>(data a,data b){return (a.bc==b.bc)?a.pc>b.pc:a.bc>b.bc;}
bool operator<(data a,data b){return (a.bc==b.bc)?a.pc<b.pc:a.bc<b.bc;}
priority_queue<data,vector<data>,greater<data> >q;
bool vis[24][24][24][24];
int dx[4]={0,1,0,-1},
    dy[4]={1,0,-1,0};
char mp[24][24],ans[160004],
     op[8]={'e','s','w','n','E','S','W','N'},
     res[160004];
data be,ed;
int pos;
int out(int x){
    int cnt=0;
    while(x!=-1){
        res[++cnt]=ans[x];
        x=from[x];
    }
    for(int i=cnt-1;i>=1;i--)printf("%c",res[i]);
    puts("");
    return 0;
}
int bfs(data BE){
    while(!q.empty())q.pop();
    q.push(BE);
    vis[BE.bx][BE.by][BE.px][BE.py]=1;
    int gg=0;
    while(!q.empty()){
        data now=q.top();q.pop();
        if(now.bx==ed.bx&&now.by==ed.by){
            out(now.pos);return 1;
        }
        for(int i=0;i<4;i++){
            int px=now.px+dx[i],py=now.py+dy[i];
            if(px>r||px<=0||py>c||py<=0||mp[px][py]=='#')continue;
            if(px==now.bx&&py==now.by){
                data g=now;
                g.bx+=dx[i];g.by+=dy[i];
                g.px=px;g.py=py;
                if(g.bx<=r&&g.bx>0&&g.by<=c&&g.by>0&&mp[g.bx][g.by]!='#'){
                    if(!vis[g.bx][g.by][g.px][g.py]){
                        vis[g.bx][g.by][g.px][g.py]=1;
                        g.pc++;g.bc++;from[pos]=now.pos;g.pos=pos;
                        q.push(g);
                        ans[pos]=op[4+i];
                        ++pos;
                    }
                }
            }
            else{
                data g=now;
                g.px=px;g.py=py;
                if(!vis[g.bx][g.by][g.px][g.py]){
                    vis[g.bx][g.by][g.px][g.py]=1;
                    g.pc++;from[pos]=now.pos;g.pos=pos;
                    q.push(g);
                    ans[pos]=op[i];
                    ++pos;
                }
            }
        }
    }
    return 0;
}
int w33ha(int CASE){
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=r;i++)scanf("%s",mp[i]+1);
    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(mp[i][j]=='S'){be.px=i;be.py=j;}
            if(mp[i][j]=='B'){be.bx=i;be.by=j;}
            if(mp[i][j]=='T'){ed.bx=i;ed.by=j;}
        }
    }
    be.pc=be.bc=0;
    be.pos=0;pos=1;
    from[0]=-1;
    printf("Maze #%d\n",CASE);
    if(!bfs(be))puts("Impossible.");
    puts("");
    return 0;
}
int LiangJiaJun(){
    int T=0;
    while(scanf("%d%d",&r,&c)!=EOF){
        if(r==0&&c==0)break;
        w33ha(++T);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值