POJ - 1475 Pushing Boxes

Pushing Boxes

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



解题思路:两个BFS即可,第一个BFS求出box的最小路径,然后在box中嵌套一个ren的bfs,求出ren到box的相邻位置的最短路径。然后合并答案即可。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

char maze[30][30];

int visbox[30][30];
int visren[30][30];

struct point{
    int x;
    int y;
    string ren;
    char dir;
    string ans;
    int rx;
    int ry;
};

struct node{
    int x;
    int y;
    char dir;
    string ans;
};

int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};

bool bfsren(string &result,node start,node end,point box){

    memset(visren,0,sizeof(visren));

    visren[start.x][start.y]=1;

    queue<node> que;

    que.push(start);

    while(!que.empty()){

        node tp=que.front();
        que.pop();

        if(tp.x==end.x&&tp.y==end.y){
            result=tp.ans;
            return true;
        }

        visren[tp.x][tp.y]=1;

        for(int i=0;i<4;i++){
            int x=tp.x+dx[i];
            int y=tp.y+dy[i];

            if(visren[x][y]==0&&(maze[x][y]!='#'&&(x!=box.x||y!=box.y))){

                node temp;
                temp.x=x;
                temp.y=y;

                if(i==0)temp.dir='n';
                if(i==1)temp.dir='e';
                if(i==2)temp.dir='s';
                if(i==3)temp.dir='w';

                temp.ans=tp.ans;
                temp.ans.push_back(temp.dir);
                que.push(temp);

                visren[temp.x][temp.y]=1;

            }
        }
    }
    return false;
}


string bfsbox(point boxstart,point boxend){

    visbox[boxstart.x][boxstart.y]=1;

    queue<point> que;

    que.push(boxstart);

    while(!que.empty()){
        point tp=que.front();
        que.pop();

        visbox[tp.x][tp.y]=1;

        if(tp.x==boxend.x&&tp.y==boxend.y){
            return tp.ans;
        }


        for(int i=0;i<4;i++){
            int x=tp.x+dx[i];
            int y=tp.y+dy[i];

            if(visbox[x][y]==1||maze[x][y]=='#')
                continue;

            node rs;
            if(i==0){
                rs.x=tp.x+1;
                rs.y=tp.y;
            }
            if(i==1){
                rs.x=tp.x;
                rs.y=tp.y-1;
            }

            if(i==2){
                rs.x=tp.x-1;
                rs.y=tp.y;
            }

            if(i==3){
                rs.x=tp.x;
                rs.y=tp.y+1;
            }



            node ren;
            ren.x=tp.rx;
            ren.y=tp.ry;

            string lu;
            if(bfsren(lu,ren,rs,tp)){
                point temp;

                if(i==0)temp.dir='N';
                if(i==1)temp.dir='E';
                if(i==2)temp.dir='S';
                if(i==3)temp.dir='W';

                temp.ans=tp.ans;

                temp.x=x;
                temp.y=y;

                temp.ren=lu;

                for(int i=0;i<temp.ren.size();i++)
                    temp.ans.push_back(temp.ren[i]);

                temp.ans.push_back(temp.dir);

                temp.rx=tp.x;
                temp.ry=tp.y;

                que.push(temp);

                visbox[temp.x][temp.y]=1;

            }
        }

    }
    string aaa;
    return aaa;
}




int N,M;

int main()
{

    int case0=1;
    while(cin>>N>>M){
        if(N==0&&M==0)
            break;

        memset(maze,0,sizeof(maze));
        memset(visbox,0,sizeof(visbox));
        memset(visren,0,sizeof(visren));

        for(int i=0;i<=M+1;i++)
            maze[0][i]='#';

        for(int i=0;i<=M+1;i++)
            maze[N+1][i]='#';

        for(int i=1;i<=N;i++)
            maze[i][0]='#';

        for(int i=1;i<=N;i++)
            maze[i][M+1]='#';


        point bs,be;

        for(int i=1;i<=N;i++)
            for(int j=1;j<=M;j++){
                cin>>maze[i][j];

                if(maze[i][j]=='T'){
                    be.x=i;
                    be.y=j;
                }
                if(maze[i][j]=='B'){
                    bs.x=i;
                    bs.y=j;
                }
                if(maze[i][j]=='S'){
                    bs.rx=i;
                    bs.ry=j;
                }
            }




        cout<<"Maze #"<<case0++<<endl;

        string result=bfsbox(bs,be);

        if(result.empty()){
            cout<<"Impossible."<<endl;
        }
        else{

            cout<<result<<endl;
        }

        cout<<endl;

    }



}

上述代码虽然能AC,但是有一组数据是过不了的。

8 9
#########
#......T#
#.S.....#
##B######
#.......#
#.......#
#.......#
#########

类似这种样例都无法通过,如果要通过,需要将标记vis改为四维,vis[box.x][box.y][ren.x][ren.y],四个状态,但是会超时…………

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值