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。先bfs”推“,在没推一次时bfs”走".

# include<iostream>
# include<cstring>
# include<string>
using namespace std;
struct node
{
int sx,sy,bx,by;
string ans;
} lxk[2][1000005];
int r,c,via[][2]={-1,0,1,0,0,-1,0,1};//注意题目要求的是n、s、w、e的顺序,因为这个wa了一次
char op[]={'n','s','w','e'};
char box[25][25];
char up(char c)
{
return (c-'a'+'A');
}
bool bfs(int tx,int ty,node &cc)//对走的过程bfs,搜索从推上一步到当前推箱子的前一格
{
if (tx<=0||tx>r||ty<=0||ty>c||box[tx][ty]=='#') return false;
bool mark[25][25];
memset(mark,false,sizeof(mark));
lxk[1][1]=cc;
int st,en;
st=1; en=1;
node a,b;
bool p=false;
while (st<=en)
{
a=lxk[1][st];
if (a.sx==tx&&a.sy==ty)
{
p=true;
cc=a;
break;
}
for (int i=0;i<4;i++)
{
int x,y;
x=a.sx+via[i][0]; y=a.sy+via[i][1];
if (x>0&&x<=r&&y>0&&y<=c&&!(x==a.bx&&y==a.by)&&!mark[x][y]&&box[x][y]!='#')
{
b=a;
b.sx=x; b.sy=y;
b.ans=b.ans+op[i];
mark[x][y]=true;
en++;
lxk[1][en]=b;
}
}
st++;
}
return p;
}
int main()
{
int i,j,tx,ty,ca=1;
while (~scanf("%d%d",&r,&c))
{
if (!r||!c) break;
for (i=1;i<=r;i++)
{
getchar();
for (j=1;j<=c;j++)
{
scanf("%c",&box[i][j]);
if (box[i][j]=='T')
{
tx=i; ty=j;
box[i][j]='.';
}
if (box[i][j]=='S')
{
lxk[0][1].sx=i; lxk[0][1].sy=j;
box[i][j]='.';
}
if (box[i][j]=='B')
{
lxk[0][1].bx=i; lxk[0][1].by=j;
box[i][j]='.';
}
}
}
lxk[0][1].ans="";
int st,en;
st=1; en=1;
node a,b;
bool p=false;
bool mark[25][25][4];
memset(mark,false,sizeof(mark));
string ans;
printf("Maze #%d\n",ca++);
while (st<=en)//对推的过程bfs
{
a=lxk[0][st];
if (a.bx==tx&&a.by==ty)
{
p=true;
ans=a.ans;
break;
}
for (i=0;i<4;i++)
{
int x,y;
x=a.bx+via[i][0]; y=a.by+via[i][1];
if (x>0&&x<=r&&y>0&&y<=c&&!mark[x][y][i]&&box[x][y]!='#')
{
b=a;
if (bfs(x-2*via[i][0],y-2*via[i][1],b))
{
b.sx=b.bx; b.sy=b.by;
b.bx=x; b.by=y;
b.ans=b.ans+up(op[i]);
mark[x][y][i]=true;
en++;
lxk[0][en]=b;
}
}
}
st++;
}
if (p) printf("%s\n\n",ans.c_str());
else printf("Impossible.\n\n");
}
return 0;
}

(poj 1475) Pushing Boxes的更多相关文章

  1. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

  2. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

  3. [poj P1475] Pushing Boxes

    [poj P1475] Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K   Special Judge Description Ima ...

  4. HDU 1475 Pushing Boxes

    Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...

  5. poj 1475 || zoj 249 Pushing Boxes

    http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...

  6. Pushing Boxes(广度优先搜索)

    题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...

  7. 『Pushing Boxes 双重bfs』

    Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...

  8. POJ1475 Pushing Boxes(双搜索)

    POJ1475 Pushing Boxes  推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...

  9. 【POJ 1475】 Pushing Boxes

    [题目链接] http://poj.org/problem?id=1475 [算法] 双重BFS [代码] #include <algorithm> #include <bitset ...

随机推荐

  1. 你需要知道的九大排序算法【Python实现】之基数排序

    八.基数排序 基本思想:基数排序(radix sort)属于"分配式排序"(distribution sort),又称"桶子法"(bucket sort)或bi ...

  2. Android源代码之Gallery专题研究(2)

    引言 上一篇文章已经解说了数据载入过程,接下来我们来看一看数据载入后的处理过程.依照正常的思维逻辑.当数据载入之后,接下来就应该考虑数据的显示逻辑. MVC显示逻辑 大家可能对J2EE的MVC架构比較 ...

  3. Android学习之路——Android四大组件之activity(二)数据的传递

    上一篇讲了activity的创建和启动,这一篇,我们来讲讲activity的数据传递 activity之间的数据传递,这里主要介绍的是activity之间简单数据的传递,直接用bundle传递基本数据 ...

  4. [React Testing] Setting up dependencies && Running tests

    To write tests for our React code, we need to first install some libraries for running tests and wri ...

  5. hdu5067Harry And Dig Machine(TSP旅行商问题)

    题目链接: huangjing 题意:给出一幅图.图中有一些点,然后从第1个点出发,然后途径全部有石头的点.最后回到原点,然后求最小距离.当初作比赛的时候不知道这就是旅行商经典问题.回来学了一下. 思 ...

  6. WEB服务健康状态检测

    #!/bin/sh #date:2015-12-07 #filename:check_web.sh #作者:lixingli #Email:1162572407@qq.com #version:v1. ...

  7. python 下的数据结构与算法---5:递归(Recursion)

    定义:递归就是不断分割整体成部分直到可以轻易解决分割出来的部分. 递归表达式三定律: 1:递归表达式必须有个最小单元     (最小单元既是停止递归调用以及能够直接运算的) 2:递归表达式在运算过程中 ...

  8. 百度前端笔试题目--css 实现一个带尖角的正方形

    今天在牛客网上看到这道题,发现自己并不会,看来自己css都没怎么学习,也不怎么会用.看了下答案,不是很明白,也在网上搜集了一些资料和解法,感觉一些同学博客上也写了一些解法和拓展,所以就在这里借鉴一下咯 ...

  9. myeclipse笔记(3):导入的项目切换jdk版本

    有时候,从外面导入的javaweb项目会访问不了,这个时候改变jdk版本就是其中解决的方法之一. 右键点击项目 --> bulid path --> configure​ ​​​ 选择需要 ...

  10. Sicily 1323. Switch text

    题目地址:1323. Switch text 思路: 题目意思不好理解呀. 题目意思是这样的:输入两个测试数据,首先,两个测试数据本身得各自前后倒转,然后两个测试数据倒转后的结果再各自对半互换,然后测 ...