Language: Default Pushing Boxes
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. 【ACM|Pushing Boxes (poj 1475 嵌套bfs)】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 EEEEEMaze #2 Impossible.Maze #3 eennwwWWWWeeeeeesswwwwwwwnNNMaze #4 swwwnnnnnneeesssSSS Source Southwestern European Regional Contest 1997 |
题意:推箱子游戏,把箱子B推到T,人初始在S,输出推箱子次数最少的方案。一开始把题目看错,以为是总步数最小,写完交了果断WA,看了discuss才知道。
思路:对箱子bfs,箱子移动一步再对人相应的bfs。实在无力吐槽,方向数组顺序必须是北南西东,调了一个下午,坑爹啊!!
代码:
#include
#include
#include
#include
#include
#include
#include
#include
推荐阅读
- ACM|HDU 5322 Hope (CDQ分治+NTT)
- 牛客算法周周练15——A、B
- Codeforces Round #609 (Div. 2)——C. Long Beautiful Integer(思维)
- 题解|【HNOI2017】大佬-dalao
- FZU - 2107题解
- ACM OJ 2036 多边形面积计算
- #|【牛客】牛客练习赛67-E-牛妹游历城市——位运算优化
- #|蓝桥杯 - [2013年第四届真题]危险系数(割点)
- ACM|回文树(自动机)(练习和总结)
- acm|扩展欧几里德算法(附证明)