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 ;
}

HDU 1475 Pushing Boxes的更多相关文章

  1. (poj 1475) Pushing Boxes

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

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

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

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

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

  4. poj 1475 || zoj 249 Pushing Boxes

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

  5. 『Pushing Boxes 双重bfs』

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

  6. [poj P1475] Pushing Boxes

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

  7. POJ1475 Pushing Boxes(双搜索)

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

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

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

  9. Pushing Boxes POJ - 1475 (嵌套bfs)

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

随机推荐

  1. WinForm使用CefSharp内嵌chrome浏览器

    先贴运行图:亲测可用!以图为证! 开始!1.创建winform程序,使用.NET 4.5.2或以上(vs2010最高支持.NET 4.0,我使用的是vs2017).这一步容易忽略,简单的说就是将项目. ...

  2. C++设计模式之状态模式(二)

    2.智能空调的设计与实现 某软件公司将开发一套智能空调系统: 系统检測到温度处于20---30度之间,则切换到常温状态:温度处于30---45度,则切换到制冷状态: 温度小于20度,则切换到制热状态. ...

  3. 【c++版数据结构】之循环单链表的实现(带头结点以及尾节点)

    所实现的循环单链表的结构例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill ...

  4. Cookies操作类

    实现代码: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c&q ...

  5. js滚动

    有选择性的重复造一些轮子,未必是件坏事.Aaron的博客上加了一个悬浮菜单,貌似显得很高大上了.虽然这类小把戏也不是头一次见了,但是从未自己写过.今天就选择性的拿这个功能写一写.下面是这个轮子的开发过 ...

  6. HIT Software Construction Lab6引发出来对锁的问题的探究

    前言 做完lab5开始做lab6了鸭,哈工大计算机学院的学生永不停歇.在做lab6的时候,我在想移动猴子是锁一整个ladder(ADT)还是只锁一个ladder的一个域Monkey数组呢?这两个好像差 ...

  7. 小程序:前端防止用户重复提交&即时消息(IM)重复发送问题解决

    背景: 最近参与开发的小程序,涉及到即时消息(IM)发送的功能: 聊天界面如下,通过键盘上的[发送]按钮,触发消息发送功能 问题发现: 功能开发完毕,进入测试流程:测试工程师反馈说: 在Android ...

  8. 21. Merge Two Sorted Lists[E]合并两个有序链表

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  9. BZOJ 4033 树形DP

    http://blog.csdn.net/mirrorgray/article/details/51123741 安利队长blog- 树形dp吧,状态挺显然的,dp[x][j]表示以x为根的子树中,选 ...

  10. 移动端web开发初探之Vuejs的简单实战

    这段时间在做的东西,是北邮人论坛APP的注册页.这个注册页是内嵌的网页,因为打算安卓和IOS平台同时使用.因此实际上就是在做移动端的web开发了. 在这过程中遇到了不少有意思的东西. DEMO的git ...