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

题目大意:推箱子。不过要求的是推的步数最少的路径,如果相同,则找到总步数最少的路径。题目分析:因为涉及到路径的优先权问题,毫无疑问就要用到优先队列。接下来就是解这道题的核心步骤了,定义状态和状态转移。显然(其实,这个“显然”是花了老长时间想明白的),状态参量就是“你”和箱子的坐标、走过的路径和已经“推”过的步数和总步数。对于这道题,状态转移反而要比定义状态容易做到。

因为箱子的位置也是变化的,所以状态参量中要有箱子坐标。一开始没注意到这一点或者说就没想明白,导致瞎耽误了很多工夫!!!

代码如下:
 1 # include<iostream>
 2 # include<cstdio>
 3 # include<queue>
 4 # include<cstring>
 5 # include<algorithm>
 6 using namespace std;
 7 struct node
 8 {
 9     int mx,my,bx,by,pt,t,h;
10     string s;
11     node(int _mx,int _my,int _bx,int _by,int _pt,int _t,string _s):mx(_mx),my(_my),bx(_bx),by(_by),pt(_pt),t(_t),s(_s){}
12     bool operator < (const node &a) const {
13         if(pt==a.pt)
14             return t>a.t;
15         return pt>a.pt;
16     }
17 };
18 int r,c,vis[25][25][25][25];
19 char mp[25][25];
20 string f[2]={"nesw","NESW"};
21 int d[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
22 bool ok(int x,int y)
23 {
24     if(x>=0&&x<r&&y>=0&&y<c)
25         return true;
26     return false;
27 }
28 void bfs(int mx,int my,int bx,int by)
29 {
30     priority_queue<node>q;
31     memset(vis,0,sizeof(vis));
32     vis[mx][my][bx][by]=1;
33     q.push(node(mx,my,bx,by,0,0,""));
34     while(!q.empty())
35     {
36         node u=q.top();
37         q.pop();
38         if(mp[u.bx][u.by]==‘T‘){
39             cout<<u.s<<endl;
40             return ;
41         }
42         for(int i=0;i<4;++i){
43             int nx=u.mx+d[i][0],ny=u.my+d[i][1];
44             if(ok(nx,ny)&&mp[nx][ny]!=‘#‘){
45                 if(nx==u.bx&&ny==u.by){
46                     int nbx=u.bx+d[i][0],nby=u.by+d[i][1];
47                     if(ok(nbx,nby)&&mp[nbx][nby]!=‘#‘&&!vis[nx][ny][nbx][nby]){
48                         vis[nx][ny][nbx][nby]=1;
49                         string s=u.s;
50                         s+=f[1][i];
51                         q.push(node(nx,ny,nbx,nby,u.pt+1,u.t+1,s));
52                     }
53                 }else{
54                     if(!vis[nx][ny][u.bx][u.by]){
55                         vis[nx][ny][u.bx][u.by]=1;
56                         string s=u.s;
57                         s+=f[0][i];
58                         q.push(node(nx,ny,u.bx,u.by,u.pt,u.t+1,s));
59                     }
60                 }
61             }
62         }
63     }
64     printf("Impossible.\n");
65 }
66 int main()
67 {
68     int cas=0,mx,my,bx,by;
69     while(scanf("%d%d",&r,&c),r+c)
70     {
71         for(int i=0;i<r;++i){
72             scanf("%s",mp[i]);
73             for(int j=0;j<c;++j){
74                 if(mp[i][j]==‘S‘)
75                     mx=i,my=j;
76                 if(mp[i][j]==‘B‘)
77                     bx=i,by=j;
78             }
79         }
80         printf("Maze #%d\n",++cas);
81         bfs(mx,my,bx,by);
82         printf("\n");
83     }
84     return 0;
85 }

做后感:一定要在想明白思路后再写代码,否则,多写无益处!!!

时间: 2024-10-10 09:44:43

POJ-1475 Pushing Boxes (BFS+优先队列)的相关文章

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

HDU 1475 Pushing Boxes

Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ID: 147564-bit integer IO format: %lld      Java class name: Main Special Judge Imagine you are standing inside a two-dimensional maze composed of sq

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

2016暑假集训补题系列——POJ 1475

POJ 1475 Pushing Boxes 题意:模拟推箱子游戏给出一个二维图,S表示人的位置,B表示箱子位置,T表示目标位置.要求最短输出路径大写字母代表推,小写字母代表走. 思路:BFS+BFS,首先对箱子进行BFS,每一次在加入新的节点时对人再进行一次BFS,找到他走到需要推箱子的地方的最小路径.(最开始不知道怎么记录路径,之前也几乎不用String,结果如此好用,还可以直接加...) #include<cstdio> #include<algorithm> #includ

【15.4.1 Pushing Boxes】双重bfs

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

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&

Pushing Boxes(广度优先搜索)

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

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,