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 题目大意:推箱子。不过要求的是推的步数最少的路径,如果相同,则找到总步数最少的路径。
题目分析:因为涉及到路径的优先权问题,毫无疑问就要用到优先队列。接下来就是解这道题的核心步骤了,定义状态和状态转移。显然(其实,这个“显然”是花了老长时间想明白的),状态参量就是“你”和箱子的坐标、走过的路径和已经“推”过的步数和总步数。对于这道题,状态转移反而要比定义状态容易做到。 因为箱子的位置也是变化的,所以状态参量中要有箱子坐标。一开始没注意到这一点或者说就没想明白,导致瞎耽误了很多工夫!!! 代码如下:
 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int mx,my,bx,by,pt,t,h;
string s;
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){}
bool operator < (const node &a) const {
if(pt==a.pt)
return t>a.t;
return pt>a.pt;
}
};
int r,c,vis[][][][];
char mp[][];
string f[]={"nesw","NESW"};
int d[][]={{-,},{,},{,},{,-}};
bool ok(int x,int y)
{
if(x>=&&x<r&&y>=&&y<c)
return true;
return false;
}
void bfs(int mx,int my,int bx,int by)
{
priority_queue<node>q;
memset(vis,,sizeof(vis));
vis[mx][my][bx][by]=;
q.push(node(mx,my,bx,by,,,""));
while(!q.empty())
{
node u=q.top();
q.pop();
if(mp[u.bx][u.by]=='T'){
cout<<u.s<<endl;
return ;
}
for(int i=;i<;++i){
int nx=u.mx+d[i][],ny=u.my+d[i][];
if(ok(nx,ny)&&mp[nx][ny]!='#'){
if(nx==u.bx&&ny==u.by){
int nbx=u.bx+d[i][],nby=u.by+d[i][];
if(ok(nbx,nby)&&mp[nbx][nby]!='#'&&!vis[nx][ny][nbx][nby]){
vis[nx][ny][nbx][nby]=;
string s=u.s;
s+=f[][i];
q.push(node(nx,ny,nbx,nby,u.pt+,u.t+,s));
}
}else{
if(!vis[nx][ny][u.bx][u.by]){
vis[nx][ny][u.bx][u.by]=;
string s=u.s;
s+=f[][i];
q.push(node(nx,ny,u.bx,u.by,u.pt,u.t+,s));
}
}
}
}
}
printf("Impossible.\n");
}
int main()
{
int cas=,mx,my,bx,by;
while(scanf("%d%d",&r,&c),r+c)
{
for(int i=;i<r;++i){
scanf("%s",mp[i]);
for(int j=;j<c;++j){
if(mp[i][j]=='S')
mx=i,my=j;
if(mp[i][j]=='B')
bx=i,by=j;
}
}
printf("Maze #%d\n",++cas);
bfs(mx,my,bx,by);
printf("\n");
}
return ;
}

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

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

  1. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

  2. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

  3. (poj 1475) Pushing Boxes

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

  4. [poj P1475] Pushing Boxes

    [poj P1475] Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K   Special Judge Description Ima ...

  5. HDU 1475 Pushing Boxes

    Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...

  6. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  7. Pushing Boxes POJ - 1475 (嵌套bfs)

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

  8. POJ1475 Pushing Boxes(BFS套BFS)

    描述 Imagine you are standing inside a two-dimensional maze composed of square cells which may or may ...

  9. poj 1475 || zoj 249 Pushing Boxes

    http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...

  10. 『Pushing Boxes 双重bfs』

    Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...

随机推荐

  1. SVN版本服务器搭建

    windows:        https://blog.csdn.net/lu1024188315/article/details/74082227 SVN 的下载地址如下 http://torto ...

  2. Linux中Postfix邮件WebMail配置(七)

    Extmail Extmail 是一个以perl语言编写,面向大容量/ISP级应用,免费的高性能Webmail软件,主要包括ExtMail.Extman两个部分的程序套件.ExtMail套件用于提供从 ...

  3. CentOS安装mysql并配置远程访问

    最近上班挺无聊,每天就是不停的重启重启重启,然后抓log.于是有事儿没事儿的看卡闲书,搞搞其他事情. 但是,公司笔记本装太多乱其八糟的东西也还是不太好. 于是,想到了我那个当VPN server的VP ...

  4. 20145321《网络对抗》Exp2 后门原理与实践

    实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启动 (3)使用MSF meterpreter生成可执行文件,利用ncat或 ...

  5. Android实践项目汇报-改(一)

    Google天气客户端NABC Need(需求):  功能性需求分析 天气预报客户端,顾名思义就是为用户提供实时准确的天气信息,方便用户出行生活.根据用户日常需求,软件完成后点开,载入界面,显示查询界 ...

  6. HTTP If-Modified-Since引发的浏览器缓存汇总

    在看Spring中HttpServlet的Service方法时,对于GET请求,代码逻辑如下: if (method.equals(METHOD_GET)) { long lastModified = ...

  7. 如何高效判断java数组是否包含某个值

    在java中,我们如何判断一个未排序数组中是否包含一个特定的值?这在java中是一个频繁非常实用的操作.那么什么样的方法才是最高效的方式?当然 ,这个问题在Stack Overflow也是得票率非常高 ...

  8. ArrayList初始化的4种方法

    In the last post we discussed about class ArrayList in Javaand it’s important methods. Here we are s ...

  9. Python3基础 tuple 创建空元组或者只有一个元素的元组 并 用乘法成倍扩充

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. printf("%f\n",5);

    http://zhidao.baidu.com/link?url=87OGcxtDa6fQoeKmk1KylLu4eIBLJSh7CA3n5NWY-Ipm9TxZViFnIui307duCXWhaM0 ...