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