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 ...
随机推荐
- JS禁用微信复制链接、禁用转发
$(function () { function onBridgeReady() { WeixinJSBridge.call('hideOptionMenu'); } if (typeof Weixi ...
- Chrome Extension 的 webRequest模块的解读
Chrome Extension 的 webRequest模块的解读 文档在此:http://developer.chrome.com/trunk/extensions/webRequest.ht ...
- 《Java程序设计》第16周周五:数据库连接 与 随机数的使用
第一部分:实验项目 项目二:数据库初步. 目的:了解Java连接数据库的步骤与方法.以及MySQL数据库的安装与使用. 目标: (1)在机房安装上MySQL数据库. 安装成功 MySQL数据库 (2) ...
- luogu1226 取余运算||快速幂
题目大意:快速求$a^b\mod p$的值. 根据二进制,令$b=\sum t_k\cdot 2^k, t\in \{0,1\}$,那么$$a^b=a^{\sum t_k\cdot 2^k}\mod ...
- viz.js操作流程
1.下载依赖的js文件,并引入 <script src="${root }/resources/js/graphviz/viz.js"></script> ...
- Swift - 将String类型的数字转换成数字类型(支持十进制、十六进制)
1,十进制的字符串转成数字 Swift中,如果要把字符串转换成数字类型(比如整型,浮点型等).可以先转成NSString类型,让后再转. 1 2 3 4 //将文本框中的值转换成数字 var i = ...
- 2017-3-11 leetcode 217 219 228
ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...
- Node.js:常用工具
ylbtech-Node.js:常用工具 1.返回顶部 1. Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简 ...
- [转载]Windows Server 2008 R2 之二十五AD RMS信任策略
原文地址:Windows Server 2008 R2 之二十五AD RMS信任策略作者:从心开始 可以通过添加信任策略,让 AD RMS 可以处理由不同的 AD RMS 群集进行权限保护的内容的授权 ...
- anaconda 使用 及 tensorflow-gpu 安装
Anaconda简易使用 创建新环境 conda create -n rcnn python=3.6 删除环境 conda remove -n rcnn --all 进入环境 conda activa ...