POJ 1475 Pushing Boxes(BFS)

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
 
  
 
  
<span style="font-size:18px;"><strong>题意:人推箱子,要求推的步数最小,其次人走的路程最少。输出人的方向(小写字母),如果人在推箱子走,则输出箱子走的方向(大写字母)。</strong></span>
<strong><span style="font-size:18px;">分析:这道题最核心的是记录状态,用vis[x][y][X][Y]:表示人在(x,y),箱子在(X,Y)的状态出现没。</span></strong>
<span style="font-size:18px;"><strong>如果这道题是要求人的步数最小的话就很简单了。但是他要求箱子的步数最小,其次才是步数最小。</strong></span>
<span style="font-size:18px;"><strong>因此我们可以在结构体中定义优先级,bfs中用优先队列维护,每次从队首取出的是最优先的状态。</strong></span>
<span style="font-size:18px;"><strong></strong></span><pre name="code" class="cpp">#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define LL __int64
const LL INF=0x3f7f7f7f;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
char c[4]={'e','w','s','n'};
char C[4]={'E','W','S','N'};
char maze[25][25];
int vis[25][25][25][25];//vis[x][y][X][Y]:表示人在(x,y),箱子在(X,Y)的状态出现没
int n,m;
struct node
{
    int x,y;//人坐标
    int bx,by;//箱子坐标
    int bnum,step;//bnum:移动箱子步数,step,人移动步数
    string str;//存下方向
    node(int xx=0,int yy=0,int bxx=0,int byy=0,string strr="",int nnum=0,int ns=0)
    {//构造函数初始化
        x=xx;y=yy;bx=bxx;by=byy;
        bnum=nnum;
        step=ns;
        str=str+strr;
    }
    friend bool operator<(node f1,node f2)
    {//定义优先级,即箱子步数小的优先,如果相等,人走的步数少的优先,用于优先队列
        if(f1.bnum==f2.bnum)
        {
            return f1.step>f2.step;
        }
        return f1.bnum>f2.bnum;
    }
};
struct node st,en,tx,ty;
inline bool ok(int x,int y)//判断图的边界
{
    return x>=1&&x<=n&&y>=1&&y<=m&&maze[x][y]!='#';
}
string ans;
int num;
void bfs()
{
    int i;
    priority_queue<node>q;//用优先队列,每次从队列中取出优先级最高的node
    q.push(st);
    memset(vis,0,sizeof(vis));
    vis[st.x][st.y][st.bx][st.by]=1;
    num=INF;
    while(!q.empty())
    {
        tx=q.top();
        q.pop();
        //cout<<tx.str<<"*****"<<endl;
        if(tx.bx==en.x&&tx.by==en.y)
        {
            num=tx.bnum;
            ans=tx.str;
            break;
        }

        for(i=0;i<4;i++)
        {
            int xx=tx.x+dir[i][0];
            int yy=tx.y+dir[i][1];
            if(ok(xx,yy))
            {
                if(xx==tx.bx&&yy==tx.by)//如果下个坐标是箱子,则人和箱子要往同一个方向走一步
                {
                    int xxx=xx+dir[i][0];//箱子下一步坐标
                    int yyy=yy+dir[i][1];
                    if(ok(xxx,yyy))
                    {
                        if(vis[xx][yy][xxx][yyy]==0)
                        {
                            string s=tx.str;
                            q.push(node(xx,yy,xxx,yyy,s.append(1,C[i]),tx.bnum+1,tx.step));
                            //s.append(),在s字符串后追加个字符
                            vis[xx][yy][xxx][yyy]=1;
                        }
                    }
                }
                else if(vis[xx][yy][tx.bx][tx.by]==0)//下个坐标不是箱子
                {
                    string s=tx.str;
                    q.push(node(xx,yy,tx.bx,tx.by,s.append(1,c[i]),tx.bnum,tx.step+1));
                    vis[xx][yy][tx.bx][tx.by]=1;
                }
            }
        }
    }
    if(num==INF)
    printf("Impossible.\n\n");
    else
    {
        cout<<ans<<endl;
        puts("");
    }
}
int main()
{
    int cas=0,i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;

        for(i=1;i<=n;i++)
        {
            scanf("%s",maze[i]+1);
            for(j=1;j<=m;j++)
            {
                if(maze[i][j]=='S')
                {
                    st.x=i;
                    st.y=j;
                }
                if(maze[i][j]=='B')
                {
                    st.bx=i;
                    st.by=j;
                }
                if(maze[i][j]=='T')
                {
                    en.x=i;
                    en.y=j;
                }
            }
        }
        st.str="";
        st.bnum=0;
        st.step=0;
        num=INF;
        printf("Maze #%d\n",++cas);
        bfs();
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值