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 by
B’ 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]=