【嵌套bfs】A - Pushing Boxes POJ - 1475 推箱子

解决推箱子问题,通过BFS算法找到将箱子推到目标位置的最短操作序列。涉及三维数组visb[][][4]记录箱子路径及方向。
摘要由CSDN通过智能技术生成

https://vjudge.net/contest/387870#problem/A

题目描述

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?
在这里插入图片描述

输入

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 byB’ and the target cell by `T’.

Input is terminated by two zeroes for r and c.

输出

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.

样例输入

1 7
SB…T
1 7
SB…#.T
7 11
###########
#T##…#
#.#.#…####
#…B…#
#.######…#
#…S…#
###########
8 4

.##.
.#…
.#…
.#.B
.##S

###T
0 0

样例输出

Maze #1
EEEEE

Maze #2
Impossible.

Maze #3
eennwwWWWWeeeeeesswwwwwwwnNN

Maze #4
swwwnnnnnneeesssSSS

自己加一组样例输入

6 5
…T
.S…
#B###


自己加的样例的输出

Maze #1
SSesswNNNNwnEEE

瞎翻译

这是一个推箱子游戏。人要把箱子推到终点,我们需要输出人走的路径(方向用nswe或NSWE表示,人不推箱子只走路时路径用小写字母nswe表示,人推箱子走路时路径用大写字母NSWE表示)

先输入两个整数row和col,表示地图大小

之后输入一个row行col列的char类型矩阵,其中#代表墙,.代表路,S代表人的起始位置,B代表箱子起始位置,T代表终点(箱子要被推到的位置)

题解

1.
先设想箱子会自己动,想象一下如何用简单的bfs求得“箱子自己动”情况下的答案
2.
在”箱子自己动“的代码基础上加入人:
假设箱子要往右动,那么先对人进行bfs,让人从自己的位置走到箱子的左边的位置,如果人可以走过去,则箱子可以往右移动。
2.5
记得人推箱子的时候人也要动,而不是仅仅箱子移动(人与箱子往相同方向移动,或者直接把人移动到箱子的上一个位置)。
3.
这是笔者自己犯的一个错误:
不能仅仅用形如visb[][]的二维数组标记箱子到过的位置,因为在某些情况下人想推箱子到终点,是需要【箱子折返】的(箱子有可能去一个地方两次或多次,可以见我自己加的输入样例)
所以要用三维数组:visb[][][4],第三维记录箱子来时的方向

4.
在箱子移动的bfs函数中嵌套人移动的bfs函数完成求解

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>

int row,col,counter;
const int maxn=25;
char cell[maxn][maxn];
bool visb[maxn][maxn][4],visp[maxn][maxn];  //visb记录箱子,visp记录人去过的位置
//人的标记方式为:先在原地标记一次,之后去了哪标哪
//箱子的标记方式为:不在原地标记,只有从一个方向(假设为2)移动到某一格(假设为i,j),则标记visb[i][j][2]为true

int step[4][2]=
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值