HDU 1475 Pushing Boxes

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

Southwestern European Regional Contest 1997

解题:二维bfs?。。。。。推箱子,主要方向是箱子移动方向,先确定箱子移动方向,再确定人要到达箱子的哪一侧,箱子移动,需要一个bfs,人到箱子的一侧需要一个bfs。。。故需要两个bfs。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 30;
 18 struct stats{
 19     int px,py,bx,by;
 20     string path;
 21 };
 22 struct node{
 23     int x,y;
 24     string path;
 25 };
 26 char mp[maxn][maxn];
 27 int r,c,box_x,box_y,pson_x,pson_y;
 28 string ans;
 29 const int dir[4][2] = {0,-1,0,1,-1,0,1,0};
 30 const char dc[4] = {‘W‘,‘E‘,‘N‘,‘S‘};
 31 const char dc2[4] = {‘w‘,‘e‘,‘n‘,‘s‘};
 32 bool check(int x,int y){
 33     return mp[x][y] != ‘#‘;
 34 }
 35 bool bfs2(int nx,int ny,int tx,int ty,int kx,int ky,string &pans){
 36     queue<node>q;
 37     bool vis[maxn][maxn] = {false};
 38     vis[nx][ny] = vis[kx][ky] = true;
 39     node now,tmp;
 40     now.x = nx;
 41     now.y = ny;
 42     now.path = "";
 43     q.push(now);
 44     while(!q.empty()){
 45         now = q.front();
 46         q.pop();
 47         if(now.x == tx && now.y == ty){
 48             pans = now.path;
 49             return true;
 50         }
 51         for(int i = 0; i < 4; i++){
 52             int zx = now.x + dir[i][0];
 53             int zy = now.y + dir[i][1];
 54             if(check(zx,zy)&&!vis[zx][zy]){
 55                 vis[zx][zy] = true;
 56                 tmp.x = zx;
 57                 tmp.y = zy;
 58                 tmp.path = now.path + dc2[i];
 59                 q.push(tmp);
 60             }
 61         }
 62     }
 63     return false;
 64 }
 65 bool bfs(){
 66     queue<stats>q;
 67     bool vis[maxn][maxn] = {false};
 68     vis[box_x][box_y] = true;
 69     stats tmp,now;
 70     now.px = pson_x;
 71     now.py = pson_y;
 72     now.bx = box_x;
 73     now.by = box_y;
 74     now.path = "";
 75     q.push(now);
 76     while(!q.empty()){
 77         now = q.front();
 78         q.pop();
 79         for(int i = 0; i < 4; i++){
 80             int nx = now.bx + dir[i][0];
 81             int ny = now.by + dir[i][1];
 82             int tx = now.bx - dir[i][0];
 83             int ty = now.by - dir[i][1];
 84             string pans = "";
 85             if(check(nx,ny)&&check(tx,ty)&&!vis[nx][ny]){
 86                 if(bfs2(now.px,now.py,tx,ty,now.bx,now.by,pans)){
 87                     vis[nx][ny] = true;
 88                     tmp.px = now.bx;
 89                     tmp.py = now.by;
 90                     tmp.bx = nx;
 91                     tmp.by = ny;
 92                     tmp.path = now.path + pans + dc[i];
 93                     if(mp[nx][ny] == ‘T‘){
 94                         ans = tmp.path;
 95                         return true;
 96                     }
 97                     q.push(tmp);
 98                 }
 99             }
100         }
101     }
102     return false;
103 }
104 int main(){
105     int cs = 1;
106     while(~scanf("%d %d",&r,&c) && r + c){
107         memset(mp,‘#‘,sizeof(mp));
108         getchar();
109         for(int i = 1; i <= r; i++){
110             for(int j = 1; j <= c; j++){
111                 mp[i][j] = getchar();
112                 if(mp[i][j] == ‘B‘){
113                     box_x = i;
114                     box_y = j;
115                 }
116                 if(mp[i][j] == ‘S‘){
117                     pson_x = i;
118                     pson_y = j;
119                 }
120             }
121             getchar();
122         }
123         printf("Maze #%d\n", cs++);
124         if(bfs()) cout<<ans<<endl;
125         else puts("Impossible.");
126         puts("");
127     }
128     return 0;
129 }

时间: 2024-10-12 23:24:21

HDU 1475 Pushing Boxes的相关文章

poj 1475 Pushing Boxes

http://poj.org/problem?id=1475 Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4662   Accepted: 1608   Special Judge Description Imagine you are standing inside a two-dimensional maze composed of square cells which may or

poj 1475 Pushing Boxes 推箱子(双bfs)

题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空位,如果有,则判断人是否能到达盒子的那一边,能的话,把盒子推到空位处,然后继续 AC代码: 1 //解题思路:先判断盒子的四周是不是有空位,如果有,则判断人是否能到达盒子的那一边,能的话,把盒子推到空位处,然后继续 2 #include<iostream> 3 #include<algori

【15.4.1 Pushing Boxes】双重bfs

[15.4.1 Pushing Boxes] 想象您正站在一个二维的迷宫中,迷宫由是正方形的方格组成,这些方格可能被岩石阻塞,也可能没有.您可以向北,南,东或西一步移到下一个方格.这些移动被称为行走(walk). 在一个空方格中放置了一个箱子,您可以挨着箱子站着,然后按这个方向推动这个箱子,这个箱子就可以被移动到一个临近的位置.这样的一个移动被称为推(push).除了推以外,箱子不可能用其他的方法被移动,这就意味着如果您把箱子推到一个角落,您就永远不能再把它从角落中推出. 一个空格被标识为目标空

Pushing Boxes(广度优先搜索)

题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不能被推动,怎样确定一条既满足最短又满足可行的箱子路径呢,其实这就是一种有限制的BFS. 对于箱子: 设现在的位置为x,y,扩展方向为dx,dy,将要到达的下一个位置为x+dx,y+dy check它是否可行: 1.不越界. 2.之前没有走过. 3.不能走到“#”上. 4.人能够从当前站立的位置到达(

【POJ 1475】 Pushing Boxes

[题目链接] http://poj.org/problem?id=1475 [算法] 双重BFS [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #incl

POJ-1475 Pushing Boxes (BFS+优先队列)

Description 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 con

poj1475 Pushing Boxes(BFS)

题目链接 http://poj.org/problem?id=1475 题意 推箱子游戏.输入迷宫.箱子的位置.人的位置.目标位置,求人是否能把箱子推到目标位置,若能则输出推的最少的路径,如果有多条步数相同的推的最少的路径,则输出总步数(人走的步数+推箱子的步数)最少的那条路径:若不能把箱子推到目标位置,则输出Impossible. 思路 先求出箱子到目标位置的最短路径(bfs_box),在bfs1推箱子的过程中,根据推的方向和箱子的位置得到人的位置,再求得人到达这个位置的最短路(bfs_per

POJ1475 Pushing Boxes(BFS套BFS)

描述 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 b

UVa 589 - Pushing Boxes

题目:二维推箱子游戏,给你箱子.人和目标的位置,输出问题的解(推箱子和行走的路径). 分析:搜索.优先队列.优先顺序为:首先保证推箱子的字数最少.然后是走的步数最少. 利用二叉堆做优先队列,在上面进行bfs即可. 说明:注意搜索时按照字典序方向枚举,不然会WA╮(╯▽╰)╭. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <