Pushing Boxes (poj 1475 嵌套bfs)

46 篇文章 0 订阅
Language:
Pushing Boxes
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 4802 Accepted: 1653 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

Source


题意:推箱子游戏,把箱子B推到T,人初始在S,输出推箱子次数最少的方案。一开始把题目看错,以为是总步数最小,写完交了果断WA,看了discuss才知道

思路:对箱子bfs,箱子移动一步再对人相应的bfs。实在无力吐槽,方向数组顺序必须是北南西东,调了一个下午,坑爹啊!!

代码:

#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 25;
const int MAXN = 2005;
const int MAXM = 2000100;
const int N = 1005;

struct Node
{
    int x,y;
    int Bx,By;
    string path;
}st,now;

int n,m,sx,sy,ex,ey,flag;
bool vis[maxn][maxn][maxn][maxn];
char mp[maxn][maxn];
int dir[4][2]={-1,0,1,0,0,-1,0,1};  //WA的检查一下方向数组,太坑爹
char a[]="nswe";
char A[]="NSWE";


bool isok(int x,int y)
{
    if (x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]!='#') return true;
    return false;
}

string bfsman(Node st,int Bx,int By,int tx,int ty,int &f)
{
    f=0;
//    printf("%d %d = %d %d = %d %d\n",Bx,By,sx,sy,tx,ty);
    Node tt,no;
    tt.Bx=Bx;tt.By=By;tt.x=st.x;tt.y=st.y;tt.path="";
    queue<Node>Q;
    vis[Bx][By][sx][sy]=true;
    Q.push(tt);
    while (!Q.empty())
    {
        tt=Q.front();Q.pop();
        if (tt.x==tx&&tt.y==ty)
        {
            f=1;
//            cout<<tt.path<<endl;
            return tt.path;
        }
        for (int i=0;i<4;i++)
        {
            no.x=tt.x+dir[i][0];
            no.y=tt.y+dir[i][1];
            if (no.x==st.Bx&&no.y==st.By) continue;
            if (isok(no.x,no.y)&&!vis[Bx][By][no.x][no.y])
            {
//                DBG;
                vis[Bx][By][no.x][no.y]=true;
                no.path=tt.path+a[i];
                Q.push(no);
            }
        }
    }
    return "";
}

void bfsbox()
{
    st.path="";
    memset(vis,false,sizeof(vis));
    vis[st.Bx][st.By][st.x][st.y]=true;
    queue<Node>Q;
    Q.push(st);
    while (!Q.empty())
    {
        st=Q.front();Q.pop();
        if (st.Bx==ex&&st.By==ey)
        {
            flag=1;
            cout<<st.path<<endl;
            return ;
        }
        for (int i=0;i<4;i++)
        {
            now.Bx=st.Bx+dir[i][0];
            now.By=st.By+dir[i][1];
            if (isok(now.Bx,now.By)&&!vis[now.Bx][now.By][st.x][st.y])
            {
                int dx=st.Bx-dir[i][0];
                int dy=st.By-dir[i][1];
                if (isok(dx,dy))
                {
                    vis[now.Bx][now.By][st.x][st.y]=true;
                    int xxx;
                    string ss=bfsman(st,now.Bx,now.By,dx,dy,xxx);
                    if (xxx)
                    {
//                        cout<<"cao "<<i<<ss<<endl;
                        now.path=st.path+ss+A[i];
                        now.x=st.Bx;now.y=st.By;
                        Q.push(now);
                    }
                    else
                        vis[now.Bx][now.By][st.x][st.y]=false;
                }
            }
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,cas=0;
    while (scanf("%d%d",&n,&m))
    {
        if (n==0&&m==0) break;
        ex=ey=-1;
        for (i=1;i<=n;i++)
        {
            scanf("%s",mp[i]+1);
            for (j=1;j<=m;j++)
            {
                if (mp[i][j]=='T')
                {
                    mp[i][j]='.';
                    ex=i;
                    ey=j;
                }
                if (mp[i][j]=='S')
                {
                    mp[i][j]='.';
                    st.x=i;st.y=j;
                }
                if (mp[i][j]=='B')
                {
                    mp[i][j]='.';
                    st.Bx=i;st.By=j;
                }
            }
        }
        flag=0;
        printf("Maze #%d\n",++cas);
//        cout<<bfsman(4,4,4,6,4,6)<<endl;
        bfsbox();
        if (!flag) printf("Impossible.\n");
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值