训练之搜索推箱子

Pushing BoxesCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

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

推箱子,需要注意的是推箱子的次数必须最小并且在此基础上人走的步数也要最小,然后输出走的方向的字符串。所以搜索找到的答案不一定是最佳答案需要加上判断答案是否最优,灿哥的博客(点击打开链接)。我使用的是优先队列,将推箱子次数最少的放在队列的上面,这样得到的答案就一定是最优的。


#include <cstdio>
#include <queue>
using namespace std;
char mp[22][22];
bool vis[22][22][22][22];//标记数组用来标记是否到达
int dir[4][2]={0,1,1,0,0,-1,-1,0},val[1000000],son[1000000],idx,ex,ey,m,n;
struct node
{
    int x,y,bx,by,id,pcnt,acnt;
    friend bool operator<(struct node a,struct node b);
}t;
bool operator<(struct node a,struct node b)
{
    if(a.pcnt!=b.pcnt) return a.pcnt>b.pcnt;
    else return a.acnt>b.acnt;
}
priority_queue<struct node>que;
void put(int x)
{
    if(son[x]!=-1)
    {
        put(son[x]);
        printf("%c",val[x]);
    }
}//重载优先队列
void bfs()
{
    idx=1;
    t.id=0;
    son[0]=-1;
    t.pcnt=0;
    t.acnt=0;
    while(que.size()) que.pop();
    que.push(t);
    while(que.size())
    {
        t=que.top();
        if(t.bx==ex && t.by==ey)//输出字母
        {
            put(t.id);
            printf("\n");
            break;
        }
        for(int i=0;i<4;i++)
        {
            t.x+=dir[i][0];
            t.y+=dir[i][1];
            if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&mp[t.x][t.y]!='#')
            {
                if(t.x==t.bx&&t.y==t.by)//如果人走到箱子的位置,说明人会推箱子
                {
                    t.bx+=dir[i][0];
                    t.by+=dir[i][1];
                    if(t.bx>=0&&t.bx<n&&t.by>=0&&t.by<m&&mp[t.bx][t.by]!='#')//箱子能被推动,即前面不是墙
                    {
                        if(vis[t.x][t.y][t.bx][t.by]==0)
                        {
                            vis[t.x][t.y][t.bx][t.by]=1;
                            int oldid=t.id;
                            son[idx]=t.id;
                            t.id=idx;
                            t.pcnt++;
                            t.acnt++;
                            if(i==0) val[idx]='E';//将字母放入一个数组中,用另一个标记数组来存,最后递归找出来
                            if(i==1) val[idx]='S';
                            if(i==2) val[idx]='W';
                            if(i==3) val[idx]='N';
                            que.push(t);
                            t.pcnt--;
                            t.acnt--;
                            idx++;
                            t.id=oldid;
                        }
                    }
                    t.bx-=dir[i][0];
                    t.by-=dir[i][1];
                }
                else//人没有推箱子
                {
                    if(vis[t.x][t.y][t.bx][t.by]==0)
                    {
                        vis[t.x][t.y][t.bx][t.by]=1;
                        int oldid=t.id;
                        son[idx]=t.id;
                        t.id=idx;
                        t.acnt++;
                        if(i==0) val[idx]='e';
                        if(i==1) val[idx]='s';
                        if(i==2) val[idx]='w';
                        if(i==3) val[idx]='n';
                        que.push(t);
                        t.acnt--;
                        idx++;
                        t.id=oldid;
                    }
                }
            }
            t.x-=dir[i][0];
            t.y-=dir[i][1];
        }
        que.pop();
    }
    if(que.empty()) printf("Impossible.\n");//不能推到终点
}
int main()
{
    int num=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)  break;
        printf("Maze #%d\n",num++);
        for(int i=0;i<n;i++) scanf("%s",mp[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                for(int p=0;p<n;p++)
                    for(int q=0;q<m;q++)
                        vis[i][j][p][q]=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(mp[i][j]=='S') t.x=i,t.y=j;//找到几个开始位置
                if(mp[i][j]=='B') t.bx=i,t.by=j;
                if(mp[i][j]=='T') ex=i,ey=j;
            }
        }
        vis[t.x][t.y][t.bx][t.by]=1;
        bfs();
        printf("\n");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值