POJ-1475 Pushing Boxes (BFS+优先队列)
Description
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
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
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+优先队列)的更多相关文章
- 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) Pushing Boxes
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...
- [poj P1475] Pushing Boxes
[poj P1475] Pushing Boxes Time Limit: 2000MS Memory Limit: 131072K Special Judge Description Ima ...
- HDU 1475 Pushing Boxes
Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- Pushing Boxes POJ - 1475 (嵌套bfs)
Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...
- POJ1475 Pushing Boxes(BFS套BFS)
描述 Imagine you are standing inside a two-dimensional maze composed of square cells which may or may ...
- 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 ...
随机推荐
- linux服务器管理员的12个有用的命令
ifconfig: 在修改内核中已有的网络接口时,你会用到ifconfig命令.这个命令通常用于系统调校和调试,但同时也可以用于在启动过程中设置接口. netstat: 对于Linux用户来说这是一个 ...
- VMware前路难测,多个厂家群雄逐鹿
以VMware为例,虚拟机巨头公布了第二财季报告所示,它第二财季收入同比增长13%,达到了21.7亿美元,而且该公司收入和每股收益均超出预期. 在人们高谈Salesforce.亚马逊等新兴云计算厂商取 ...
- C++系统时间及字符串转换参考资料
https://msdn.microsoft.com/en-us/library/a442x3ye.aspx https://msdn.microsoft.com/en-us/library/fe06 ...
- 响应式瀑布流插件Grid-A-Licious
Grid-A-Licious是一款遵守MIT协议的响应式瀑布流插件.该插件总代码行不超过400行,实现很巧妙,使用时也很流畅.实现原理也很简单,根据屏幕宽度和参数中设置的列宽度以及每项之间的间隔宽度, ...
- 计算概论(A)/基础编程练习1(8题)/7:奇数求和
#include<stdio.h> int main() { // 输入非负整数 int m, n; scanf("%d %d", &m, &n); / ...
- SQL学习之SQL注入总结
Sql注入定义: 就是通过把sql命令插入到web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行的sql命令的目的. sql注入分类: 基于联合查询 基于错误回显 基于盲注,分时间盲 ...
- JavaScript 实现全选 / 反选功能
JavaScript 实现全选 / 反选功能 版权声明:未经授权,内容严禁转载! 构建主体界面 编写 HTML 代码 和 CSS 代码,设计主题界面 <style> #user { wid ...
- troubleshooting-windows 在 CDH集群环境读取 Hive 表 KrbException: Cannot locate default realm
KrbException: Cannot locate default realm 解决办法 1)拷贝需要组件的配置文件到项目中的 /resources/目录.如hadoop,目录/etc/hadoo ...
- windows 上安装redis和windows上redis与php扩展
1.下载redis压缩包(自己选择想要的版本,1,2地址任意选一个) 下载window版本地址1:https://github.com/dmajkic/redis/downloads 下载window ...
- phpMyAdmin本地文件包含漏洞
4 phpMyAdmin本地文件包含漏洞 4.1 摘要 4.1.1 漏洞简介 phpMyAdmin是一个web端通用MySQL管理工具,上述版本在/libraries/gis/pma_gis_fact ...