HDU 1475 Pushing Boxes
Pushing Boxes
This problem will be judged on PKU. Original ID: 1475
64-bit integer IO format: %lld Java class name: Main
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
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
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
#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的更多相关文章
- (poj 1475) Pushing Boxes
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
- poj 1475 Pushing Boxes 推箱子(双bfs)
题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...
- poj 1475 || zoj 249 Pushing Boxes
http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...
- 『Pushing Boxes 双重bfs』
Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...
- [poj P1475] Pushing Boxes
[poj P1475] Pushing Boxes Time Limit: 2000MS Memory Limit: 131072K Special Judge Description Ima ...
- POJ1475 Pushing Boxes(双搜索)
POJ1475 Pushing Boxes 推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...
- Pushing Boxes(广度优先搜索)
题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...
- Pushing Boxes POJ - 1475 (嵌套bfs)
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...
随机推荐
- C算法与数据结构-线性表的应用,多项式求和---ShinePans
/*---上机作业作业,二项式加法---*/ /*---By 潘尚 ---*/ /*---日期: 2014-5-8 . ---*/ /*---题目:---*/ //如果有两个稀疏多项式A和B,设计算法 ...
- ubuntu下创建第一个rails应用程序
一.创建一个新的应用程序 在控制台输入 > rails new demo create create README.rdoc create Rakefile create config.ru ...
- Spring配置之OpenSessionInViewFilter
转自:https://www.cnblogs.com/blogonfly/articles/3991782.html 参考: OpenSessionInViewFilter作用及配置:http://w ...
- redis实现计数--------Redis increment
经理提出新的需求,需要知道每天微信推送了多少条模板消息,成功多少条,失败多少条,想到用Redis缓存,网上查了一些资料,Redis中有方法increment,测试代码如下 Controller imp ...
- php---依赖倒转(反转控制)原则
一.简介 依赖注入和控制反转说的实际上是同一个东西,它们是一种设计模式,这种设计模式用来减少程序间的耦合 优点:使用依赖注入,最重要的一点好处就是有效的分离了对象和它所需要的外部资源,使得它们松散耦合 ...
- MYSQL5.6和5.7编译标准化安装与配置
文档结构图如下: 一.前期规划 1.软件环境以及说明 操作系统:RedHat Linux 6.7 64位 数 据 库:MYSQL5.6.38/5.7.20 MySQL 5.6:初始化数据时需要进到家目 ...
- Email非正则表达式的判断
判断一个邮件是否合法,我们除了可以使用正则表达四以外还可以利用,Email地址的规律进行判断,那么一个合法的Email有哪些要求昵?具体如下: 必须有@与. @必须在第一个.之前 @与.不能连在一起 ...
- POJ 3122 二分
大致题意: 就是公平地分披萨pie 我生日,买了n个pie,找来f个朋友,那么总人数共f+1人 每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就 ...
- ADO.NET增删改
static void Main1(string[] args) {添加造连接字符串string connstring = "server=.;database=mydb;user=sa;p ...
- JS学习笔记(一)JS处理JSON数据
[摘抄]将JSON字符串转换为json对象的方法.在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如:J ...