一个箱子的推箱子算法

该博客讨论了如何解决推箱子问题,通过使用优先队列来找出使箱子到达目标位置所需的最小推动次数。当存在多个达到最小推动次数的解决方案时,优先选择总步数最少的路径。给出了C++代码实现。
摘要由CSDN通过智能技术生成

Pushing Boxes

Description

Imagine you arestanding inside a two-dimensional maze composed of square cells which may ormay not be filled with rock. You can move north, south, east or west one cellat a step. These moves are called walks. 
One of the empty cells contains a box which can be moved to an adjacent freecell 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 bypushing, which means that if you push it into a corner you can never get it outof the corner again. 

One of the empty cells is marked as the target cell. Your job is to bring thebox to the target cell by a sequence of walks and pushes. As the box is veryheavy, you would like to minimize the number of pushes. Can you write a programthat will work out the best such sequence? 

Input

The input containsthe descriptions of several mazes. Each maze description starts with a linecontaining two integers r and c (both <= 20) representing the number of rowsand columns of the maze. 

Following this are r lines each containing c characters. Each characterdescribes one cell of the maze. A cell full of rock is indicated by a `#' andan 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

For each maze inthe 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 ismore than one such sequence, choose the one that minimizes the number of totalmoves (walks and pushes). If there is still more than one such sequence, anyone is acceptable. 

Print the sequence as a string of the characters N, S, E, W, n, s, e and wwhere uppercase letters stand for pushes, lowercase lett

´问题描述: 码头仓库是划分为n×m个格子的矩形阵列。有公共边的格子是相邻格子。当前仓库中 有的格子是空闲的;有的格子则已经堆放了沉重的货物。由于堆放的货物很重,单凭仓库管 理员的力量是无法移动的。仓库管理员有一项任务,要将一个箱子推到指定的格子上去。 管理员可以在仓库中移动,但不能跨过已经堆放了货物的格子。管理员站在与箱子相对的空 闲格子上时,可以做一次推动,把箱子推到另一相邻的空闲格子。推箱时只能向管理员的对 面方向推。由于要推动的箱子很重,仓库管理员想尽量减少推箱子的次数。 ´编程任务: 对于给定的仓库布局,以及仓库管理员在仓库中的位置和箱子的开始位置和目标位置, 设计一个推箱子问题的分支限界法, 计算出仓库管理员将箱子从开始位置推到目标位置所 需的最少推动次数。 ´数据输入: 由文件input.txt提供输入数据。输入文件第 1 行有 2个正整数 n和 m(1<=n,m<=100) , 表示仓库是n×m个格子的矩形阵列。接下来有 n行,每行有 m个字符,表示格子的状态。 S 表示格子上放了不可移动的沉重货物; w 表示格子空闲; M 表示仓库管理员的初始位置; P 表示箱子的初始位置; K 表示箱子的目标位置。 ´结果输出: 将计算出的最少推动次数输出到文件 output.txt。如果仓库管理员无法将箱子从开始位 置推到目标位置则输出“No solution!” 。 输入文件示例 输出文件示例 input.txt output.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值