Pushing Boxes POJ - 1475 (嵌套bfs)
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 题意:推箱子,要求出箱子移动最少(其次是人移动最少)的路径
思路:
①最优先的是箱子移动的步数,我们可以先对箱子进行bfs,则可以找到箱子移动最少的路径
②箱子移动最少但不代表合法,(因为也许这条路径中箱子在某处处于角落),其次是除了箱子的步数,人的步数也是尽可能的小,那么对于人移动的最小路径,我们仍然进行一次bfs
③首先是箱子bfs枚举四个方向(x+ways【i】【0】,y+ways【i】【1】),然后为了验证箱子移动是否合法,对人进行一次bfs,从人起点到(x-ways【i】【0】,y-ways【i】【1】),如果能到就返回1(一定最短),说明合法。
因为箱子需要向一个方向移动时,人必须站在(x-ways【i】【0】,y-ways【i】【1】)的位置推,然后才是箱子和人一起向该方向在移动一次
④至于路劲记录,可以用记录前置路径,然后递归的方式,(这样的话需要记录下所有移动的路径信息,从答案向前推到)。
也可以在结构体中加入string,然后 每次移动 = 原来路径+人移动路径 + 人箱子移动路径 注:局部变量不会自动初始化,全局变量才会。
#include<iostream>
#include<cstdio>
#include<string.h>;
#include<queue>
using namespace std; int r,c;
char maps[][];
int ways[][] = {,,-,,,,,-};
string forw[] = {"s","n","e","w"};
string FORW[] = {"S","N","E","W"};
string ans; struct Node
{
int sx,sy;
int bx,by;
string path;
Node(int sx=,int sy=,int bx=,int by=,string path=""):sx(sx),sy(sy),bx(bx),by(by),path(path) {}
}start; struct Peo
{
int x,y;
string path;
Peo(int x=,int y=,string path=""):x(x),y(y),path(path) {}
}; bool check(int x,int y)
{
if(x < || x > r || y < || y > c)
return ;
if(maps[x][y] == '#')
return ;
return ;
} bool cal(int sx,int sy,int ex,int ey,int tx,int ty,string &path)
{
queue<Peo>que;
while(!que.empty())
que.pop();
que.push(Peo(sx,sy));
bool vis[][];
memset(vis,,sizeof(vis));
vis[tx][ty] = vis[sx][sy] = ;
while(!que.empty())
{
Peo tmp = que.front();
que.pop();
if(tmp.x == ex && tmp.y == ey)
{
path = tmp.path;
return ;
}
for(int i=; i<; i++)
{
int xx = tmp.x + ways[i][];
int yy = tmp.y + ways[i][];
if(check(xx,yy) && !vis[xx][yy])
{
string t_path = tmp.path + forw[i];
vis[xx][yy] = ;
que.push(Peo(xx,yy,t_path));
}
}
}
return ;
} bool bfs()
{
bool vis[][];
memset(vis,,sizeof(vis));
queue<Node>que;
while(!que.empty())
que.pop();
start.path = "";
que.push(start);
vis[start.bx][start.by] = ;
while(!que.empty())
{
Node tmp = que.front();
que.pop();
for(int i=; i<; i++)
{
int bx = tmp.bx + ways[i][];
int by = tmp.by + ways[i][];
int ex = tmp.bx - ways[i][];
int ey = tmp.by - ways[i][];
string path = "";
if(check(bx,by) && check(ex,ey) && !vis[bx][by])
{
if(cal(tmp.sx,tmp.sy,ex,ey,tmp.bx,tmp.by,path))
{
vis[bx][by] = ;
string t_path = tmp.path + path + FORW[i];
if(maps[bx][by] == 'T')
{
ans = t_path;
return ;
}
que.push(Node(tmp.bx,tmp.by,bx,by,t_path));
}
}
}
}
return ;
} int main()
{ int cas = ;
while(~scanf("%d%d",&r,&c) && r && c)
{
char s[];
for(int i=; i<=r; i++)
{
scanf("%s",&s);
for(int j=; j<=c; j++)
{
maps[i][j] = s[j-];
if(maps[i][j] == 'S')
{
start.sx = i;
start.sy = j;
}
else if(maps[i][j] == 'B')
{
start.bx = i;
start.by = j;
}
}
}
printf("Maze #%d\n", ++cas);
int flag = bfs();
if(!flag)
printf("Impossible.\n");
else
cout<< ans << endl;
puts("");
}
}
Pushing Boxes POJ - 1475 (嵌套bfs)的更多相关文章
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
- poj 1475 || zoj 249 Pushing Boxes
http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...
- [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 ...
- 『Pushing Boxes 双重bfs』
Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...
- Pushing Boxes(广度优先搜索)
题目传送门 首先说明我这个代码和lyd的有点不同:可能更加复杂 既然要求以箱子步数为第一关键字,人的步数为第二关键字,那么我们可以想先找到箱子的最短路径.但单单找到箱子的最短路肯定不行啊,因为有时候不 ...
- POJ1475 Pushing Boxes(双搜索)
POJ1475 Pushing Boxes 推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...
- poj 1475 Pushing Boxes 推箱子(双bfs)
题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...
- 【POJ 1475】 Pushing Boxes
[题目链接] http://poj.org/problem?id=1475 [算法] 双重BFS [代码] #include <algorithm> #include <bitset ...
随机推荐
- list_test
#! -*- coding:utf-8 -*-"""len() ,len(list)方法返回列表元素个数,list -- 要计算元素个数的列表,返回值,返回列表元素个数元 ...
- Struts2中类数据封装的方式
第一种方式:属性驱动提供对应属性的set方法进行数据的封装.表单的哪些属性需要封装数据,那么在对应的Action类中提供该属性的set方法即可.表单中的数据提交,最终找到Action类中的setXxx ...
- 使用 mod_rewrite 来修改 Confluence 6 的 URLs
备注:这个页面的文档是 Apache 的配置,而不是 Confluence 自己的配置.Atlassian 将会对 Confluence 的配置提供支持,但是我们不能保证能够对你所有在配置 Apach ...
- Confluence 6 通过 SSL 或 HTTPS 运行 - 确定你的证书路径
在默认的情况下,Tomcat 希望 keystore 文件被命名为 .keystore 文件,同时这个文件应该放置在 Tomcat 运行的 home 目录中(这个目录可能与你自己的 Home 目录的路 ...
- Confluence 6 SQL Server 数据库驱动修改
从 Confluence 6.4 开始,我们使用官方的 Microsoft SQL Server JDBC 驱动来替换掉开源的 jTDS 驱动.从这个版本开始所有的安装都会默认使用官方的 Micros ...
- LeetCode(84): 柱状图中最大的矩形
Hard! 题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度 ...
- 如何使用VisualSVN Server建立版本库
首先打开VisualSVN Server Manager,如图: 可以在窗口的右边看到版本库的一些信息,比如状态,日志,用户认证,版本库等.要建立版本库,需要右键单击左边窗口的Repositores, ...
- NPOI操作Excel(一)--NPOI基础
用C#读取Excel的方法有很多中,由于近期工作需要,需要解析的Excel含有合并单元格以及背景色等特殊要求,故在网上查了一些关于读Excel的方法的优缺点,觉得NPOI能满足我的需要,所以搜索了一些 ...
- springboot动态多数据源切换
application-test.properties #datasource -- mysql multiple.datasource.master.url=jdbc:mysql://localho ...
- 论文阅读笔记三十三:Feature Pyramid Networks for Object Detection(FPN CVPR 2017)
论文源址:https://arxiv.org/abs/1612.03144 代码:https://github.com/jwyang/fpn.pytorch 摘要 特征金字塔是用于不同尺寸目标检测中的 ...