Pushing Boxes

Time Limit: 2000ms
Memory Limit: 131072KB

This problem will be judged on PKU. Original ID: 1475
64-bit integer IO format: %lld      Java class name: Main

Special Judge
 
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

 
解题:二维bfs?。。。。。推箱子,主要方向是箱子移动方向,先确定箱子移动方向,再确定人要到达箱子的哪一侧,箱子移动,需要一个bfs,人到箱子的一侧需要一个bfs。。。故需要两个bfs。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct stats{
int px,py,bx,by;
string path;
};
struct node{
int x,y;
string path;
};
char mp[maxn][maxn];
int r,c,box_x,box_y,pson_x,pson_y;
string ans;
const int dir[][] = {,-,,,-,,,};
const char dc[] = {'W','E','N','S'};
const char dc2[] = {'w','e','n','s'};
bool check(int x,int y){
return mp[x][y] != '#';
}
bool bfs2(int nx,int ny,int tx,int ty,int kx,int ky,string &pans){
queue<node>q;
bool vis[maxn][maxn] = {false};
vis[nx][ny] = vis[kx][ky] = true;
node now,tmp;
now.x = nx;
now.y = ny;
now.path = "";
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
if(now.x == tx && now.y == ty){
pans = now.path;
return true;
}
for(int i = ; i < ; i++){
int zx = now.x + dir[i][];
int zy = now.y + dir[i][];
if(check(zx,zy)&&!vis[zx][zy]){
vis[zx][zy] = true;
tmp.x = zx;
tmp.y = zy;
tmp.path = now.path + dc2[i];
q.push(tmp);
}
}
}
return false;
}
bool bfs(){
queue<stats>q;
bool vis[maxn][maxn] = {false};
vis[box_x][box_y] = true;
stats tmp,now;
now.px = pson_x;
now.py = pson_y;
now.bx = box_x;
now.by = box_y;
now.path = "";
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
for(int i = ; i < ; i++){
int nx = now.bx + dir[i][];
int ny = now.by + dir[i][];
int tx = now.bx - dir[i][];
int ty = now.by - dir[i][];
string pans = "";
if(check(nx,ny)&&check(tx,ty)&&!vis[nx][ny]){
if(bfs2(now.px,now.py,tx,ty,now.bx,now.by,pans)){
vis[nx][ny] = true;
tmp.px = now.bx;
tmp.py = now.by;
tmp.bx = nx;
tmp.by = ny;
tmp.path = now.path + pans + dc[i];
if(mp[nx][ny] == 'T'){
ans = tmp.path;
return true;
}
q.push(tmp);
}
}
}
}
return false;
}
int main(){
int cs = ;
while(~scanf("%d %d",&r,&c) && r + c){
memset(mp,'#',sizeof(mp));
getchar();
for(int i = ; i <= r; i++){
for(int j = ; j <= c; j++){
mp[i][j] = getchar();
if(mp[i][j] == 'B'){
box_x = i;
box_y = j;
}
if(mp[i][j] == 'S'){
pson_x = i;
pson_y = j;
}
}
getchar();
}
printf("Maze #%d\n", cs++);
if(bfs()) cout<<ans<<endl;
else puts("Impossible.");
puts("");
}
return ;
}

最新文章

  1. web开发过程中经常用到的一些公共方法及操作
  2. for_each使用方法详解[转]
  3. TO~亲爱的自己
  4. android 中如何获取camera当前状态
  5. mysql开启外联方法
  6. zoj1108 FatMouse's Speed
  7. 快速排序的时间复杂度nlogn是如何推导的??
  8. Opencv常用函数
  9. 怎么样刷新frameset的整个页面
  10. 记录下项目中常用到的JavaScript/JQuery代码二(大量实例)
  11. Hbase命令
  12. TCP 三次握爪 四次挥手
  13. Python安装及IDE激活
  14. vue 之 加载 iframe 的处理
  15. day09 MapReduce
  16. POI事件模型处理execl导入功能(只支持07版本的execl)
  17. CSS基础问题
  18. 对于session,request,cookie的理解
  19. centos6 内网可达,外网不可达 Network is unreachable
  20. 详解 nginx location ~ .*\.(js|css)?$ 什么意思?

热门文章

  1. tomcat下载及启动
  2. fzu 1075 分解素因子
  3. 第一次Java作业——简单的登录界面
  4. tflearn anaconda 安装过程记录
  5. 【转】In ASP.NET using jQuery Uploadify upload attachment
  6. Netty简单介绍(非原创)
  7. O - Masha and Bears
  8. RAR 5.50 控制台使用记录
  9. VPU硬编码
  10. Linux od与hexdump命令