python - 如何找到将项目移动到堆栈中某个位置的最小移动次数?
问题描述
给定一组 NXP 堆栈,其中 N 是堆栈数,P 是堆栈容量,我如何计算从位置 A 的某个节点移动到某个任意位置 B 所需的最小交换次数?我正在设计一款游戏,最终目标是对所有堆栈进行排序,使它们的颜色都相同。
# Let "-" represent blank spaces, and assume the stacks are
stacks = [
['R', 'R', 'R', 'R'],
['Y', 'Y', 'Y', 'Y'],
['G', 'G', 'G', 'G'],
['-', '-', '-', 'B'],
['-', 'B', 'B', 'B']
]
如果我想在stacks[1][1]
这样的地方插入一个“B” stacks[1] = ["-", "B", "Y", "Y"]
。我如何确定这样做所需的最少移动次数?
我一直在研究多种方法,我尝试过从一个状态生成所有可能的移动的遗传算法,对它们进行评分,然后继续沿着最佳评分路径,我还尝试运行 Djikstra 的算法来解决问题. 它看起来简单得令人沮丧,但我想不出一种方法让它在指数时间内运行。我是否缺少适用于此处的算法?
编辑
我编写了这个函数来计算所需的最小移动次数: stacks: List of Characters 表示堆栈中的片段,stacks[0][0] 是堆栈的顶部 [0] stack_ind: 的索引将要添加到堆栈中的片段 needs_piece: 应该添加到堆栈中的片段 needs_index: 片段应该位于的索引
def calculate_min_moves(stacks, stack_ind, needs_piece, needs_index):
# Minimum moves needed to empty the stack that will receive the piece so that it can hold the piece
num_removals = 0
for s in stacks[stack_ind][:needs_index+1]:
if item != "-":
num_removals += 1
min_to_unlock = 1000
unlock_from = -1
for i, stack in enumerate(stacks):
if i != stack_ind:
for k, piece in enumerate(stack):
if piece == needs_piece:
if k < min_to_unlock:
min_to_unlock = k
unlock_from = i
num_free_spaces = 0
free_space_map = {}
for i, stack in enumerate(stacks):
if i != stack_ind and i != unlock_from:
c = stack.count("-")
num_free_spaces += c
free_space_map[i] = c
if num_removals + min_to_unlock <= num_free_spaces:
print("No shuffling needed, there's enough free space to move all the extra nodes out of the way")
else:
# HERE
print("case 2, things need shuffled")
编辑:堆栈上的测试用例:
stacks = [
['R', 'R', 'R', 'R'],
['Y', 'Y', 'Y', 'Y'],
['G', 'G', 'G', 'G'],
['-', '-', '-', 'B'],
['-', 'B', 'B', 'B']
]
Case 1: stacks[4][1] should be 'G'
Move 'B' from stacks[4][1] to stacks[3][2]
Move 'G' from stacks[2][0] to stacks[4][1]
num_removals = 0 # 'G' is directly accessible as the top of stack 2
min_to_unlock = 1 # stack 4 has 1 piece that needs removed
free_spaces = 3 # stack 3 has free spaces and no pieces need moved to or from it
moves = [[4, 3], [2, 4]]
min_moves = 2
# This is easy to calculate
Case 2: stacks[0][3] should be 'B'
Move 'B' from stacks[3][3] to stack[4][0]
Move 'R' from stacks[0][0] to stacks[3][3]
Move 'R' from stacks[0][1] to stacks[3][2]
Move 'R' from stacks[0][2] to stacks[3][1]
Move 'R' from stacks[0][3] to stacks[3][0]
Move 'B' from stacks[4][0] to stacks[0][3]
num_removals = 0 # 'B' is directly accessible
min_to_unlock = 4 # stack 0 has 4 pieces that need removed
free_spaces = 3 # If stack 3 and 4 were switched this would be 1
moves = [[3, 4], [0, 3], [0, 3], [0, 3], [0, 3], [4, 0]]
min_moves = 6
#This is hard to calculate
实际的代码实现并不是困难的部分,它决定了如何实现一种算法来解决我正在努力解决的问题。
根据@YonIif 的要求,我为这个问题创建了一个要点。
当它运行时,它会生成一个随机堆栈数组,并在随机位置选择一个需要插入到随机堆栈中的随机片段。
运行它会将这种格式的内容打印到控制台。
All Stacks: [['-', '-', 'O', 'Y'], ['-', 'P', 'P', 'O'], ['-', 'P', 'O', 'Y'], ['Y', 'Y', 'O', 'P']]
Stack 0 is currently ['-', '-', 'O', 'Y']
Stack 0 should be ['-', '-', '-', 'P']
状态更新
我非常决心以某种方式解决这个问题。
请记住,有一些方法可以最大限度地减少案例数量,例如评论中提到的@Hans Olsson。我最近解决这个问题的方法是开发一组类似于上述规则的规则,并将它们用于世代算法。
规则如:
永远不要逆转动作。从 1->0 然后 0->1 (没有意义)
永远不要连续移动一块。永远不要从 0 -> 1 然后 1 -> 3 移动
给定从 stacks[X] 到 stacks[Y] 的一些移动,然后是一些移动次数,然后是从 stacks[Y] 到 stacks[Z] 的移动,如果 stacks[Z] 处于与移动时相同的状态从 stacks[X] 到 stacks[Y] 发生,可以通过从 stacks[X] 直接移动到 stacks[Z] 来消除移动
目前,我正在尝试创建足够的规则来解决这个问题,以最大限度地减少“有效”移动的数量,从而可以使用分代算法计算答案。如果有人能想到其他规则,我有兴趣在评论中听到它们。
更新
感谢@RootTwo 的回答,我有了一些突破,我将在这里概述。
走向突破
将目标高度定义为目标块必须放置在目标堆栈中的深度。
每当某个目标块放置在 index <= stack_height - 目标高度时,总会有一条通过 clear_path() 方法获得胜利的最短路径。
Let S represent some solid Piece.
IE
Stacks = [ [R, R, G], [G, G, R], [-, -, -] ]
Goal = Stacks[0][2] = R
Goal Height = 2.
Stack Height - Goal Height = 0
给定一些这样stack[0] = R
的筹码,游戏就赢了。
GOAL
[ [ (S | -), (S | -), (S | -) ], [R, S, S], [(S | - ), (S | -), (S | -)] ]
由于已知它们始终至少有可用的 stack_height 空白空间,因此最坏的可能情况是:
[ [ S, S, !Goal ], [R, S, S], [-, -, -]
因为我们知道球门不能在球门目的地,否则比赛就赢了。在这种情况下,所需的最小移动数将是移动:
(0, 2), (0, 2), (0, 2), (1, 0)
Stacks = [ [R, G, G], [-, R, R], [-, -, G] ]
Goal = Stack[0][1] = R
Stack Height - Goal Height = 1
给定一些这样stack[1] = R
的筹码,游戏就赢了。
GOAL
[ [ (S | -), (S | -), S], [ (S | -), R, S], [(S | -), (S | -), (S | -)]
我们知道至少有 3 个空格可用,所以最坏的情况是:
[ [ S, !Goal, S], [S, R, S], [ -, -, - ]
在这种情况下,最小的移动数将是移动:
(1, 2), (0, 2), (0, 2), (1, 0)
这将适用于所有情况。
因此,问题已被简化为找到将球门块放置在球门高度或高于球门高度所需的最小移动次数的问题。
这将问题拆分为一系列子问题:
当目标堆栈有其可访问的块!= 目标块时,确定该块是否存在有效位置,或者在交换另一块时该块是否应该保留在那里。
当目标堆栈有其可访问的块 == 目标块时,确定是否可以将其移除并放置在所需的目标高度,或者在交换另一个块时是否应该保留该块。
当上述两种情况需要交换另一块时,确定交换哪些块以增加目标块以使目标块有可能达到目标高度。
目标堆栈应始终首先评估其案例。
IE
stacks = [ [-, R, G], [-, R, G], [-, R, G] ]
Goal = stacks[0][1] = G
首先检查目标堆栈会导致:
(0, 1), (0, 2), (1, 0), (2, 0) = 4 Moves
忽略目标堆栈:
(1, 0), (1, 2), (0, 1), (0, 1), (2, 0) = 5 Moves
解决方案
我想出了两个选择,但没有一个能够及时解决案例 2。第一个选项是使用带有字符串距离度量的 A* 作为 h(n),第二个选项是 IDA*。我测试了许多字符串相似性度量,我在我的方法中使用了 smith-waterman。我已更改您的符号以更快地处理问题。我在每个数字的末尾添加了数字,以检查一块是否移动了两次。
以下是我测试过的案例:
start = [
['R1', 'R2', 'R3', 'R4'],
['Y1', 'Y2', 'Y3', 'Y4'],
['G1', 'G2', 'G3', 'G4'],
['B1'],
['B2', 'B3', 'B4']
]
case_easy = [
['R', 'R', 'R', 'R'],
['Y', 'Y', 'Y', 'Y'],
['G', 'G', 'G'],
['B', 'B'],
['B', 'B', 'G']
]
case_medium = [
['R', 'R', 'R', 'R'],
['Y', 'Y', 'Y', 'B'],
['G', 'G', 'G'],
['B'],
['B', 'B', 'G', 'Y']
]
case_medium2 = [
['R', 'R', 'R' ],
['Y', 'Y', 'Y', 'B'],
['G', 'G' ],
['B', 'R', 'G'],
['B', 'B', 'G', 'Y']
]
case_hard = [
['B'],
['Y', 'Y', 'Y', 'Y'],
['G', 'G', 'G', 'G'],
['R','R','R', 'R'],
['B','B', 'B']
]
这是 A* 代码:
from copy import deepcopy
from heapq import *
import time, sys
import textdistance
import os
def a_star(b, goal, h):
print("A*")
start_time = time.time()
heap = [(-1, b)]
bib = {}
bib[b.stringify()] = b
while len(heap) > 0:
node = heappop(heap)[1]
if node == goal:
print("Number of explored states: {}".format(len(bib)))
elapsed_time = time.time() - start_time
print("Execution time {}".format(elapsed_time))
return rebuild_path(node)
valid_moves = node.get_valid_moves()
children = node.get_children(valid_moves)
for m in children:
key = m.stringify()
if key not in bib.keys():
h_n = h(key, goal.stringify())
heappush(heap, (m.g + h_n, m))
bib[key] = m
elapsed_time = time.time() - start_time
print("Execution time {}".format(elapsed_time))
print('No Solution')
这是 IDA* 代码:
#shows the moves done to solve the puzzle
def rebuild_path(state):
path = []
while state.parent != None:
path.insert(0, state)
state = state.parent
path.insert(0, state)
print("Number of steps to solve: {}".format(len(path) - 1))
print('Solution')
def ida_star(root, goal, h):
print("IDA*")
start_time = time.time()
bound = h(root.stringify(), goal.stringify())
path = [root]
solved = False
while not solved:
t = search(path, 0, bound, goal, h)
if type(t) == Board:
solved = True
elapsed_time = time.time() - start_time
print("Execution time {}".format(elapsed_time))
rebuild_path(t)
return t
bound = t
def search(path, g, bound, goal, h):
node = path[-1]
time.sleep(0.005)
f = g + h(node.stringify(), goal.stringify())
if f > bound: return f
if node == goal:
return node
min_cost = float('inf')
heap = []
valid_moves = node.get_valid_moves()
children = node.get_children(valid_moves)
for m in children:
if m not in path:
heappush(heap, (m.g + h(m.stringify(), goal.stringify()), m))
while len(heap) > 0:
path.append(heappop(heap)[1])
t = search(path, g + 1, bound, goal, h)
if type(t) == Board: return t
elif t < min_cost: min_cost = t
path.pop()
return min_cost
class Board:
def __init__(self, board, parent=None, g=0, last_moved_piece=''):
self.board = board
self.capacity = len(board[0])
self.g = g
self.parent = parent
self.piece = last_moved_piece
def __lt__(self, b):
return self.g < b.g
def __call__(self):
return self.stringify()
def __eq__(self, b):
if self is None or b is None: return False
return self.stringify() == b.stringify()
def __repr__(self):
return '\n'.join([' '.join([j[0] for j in i]) for i in self.board])+'\n\n'
def stringify(self):
b=''
for i in self.board:
a = ''.join([j[0] for j in i])
b += a + '-' * (self.capacity-len(a))
return b
def get_valid_moves(self):
pos = []
for i in range(len(self.board)):
if len(self.board[i]) < self.capacity:
pos.append(i)
return pos
def get_children(self, moves):
children = []
for i in range(len(self.board)):
for j in moves:
if i != j and self.board[i][-1] != self.piece:
a = deepcopy(self.board)
piece = a[i].pop()
a[j].append(piece)
children.append(Board(a, self, self.g+1, piece))
return children
用法:
initial = Board(start)
final1 = Board(case_easy)
final2 = Board(case_medium)
final2a = Board(case_medium2)
final3 = Board(case_hard)
x = textdistance.gotoh.distance
a_star(initial, final1, x)
a_star(initial, final2, x)
a_star(initial, final2a, x)
ida_star(initial, final1, x)
ida_star(initial, final2, x)
ida_star(initial, final2a, x)
推荐阅读
- angular - 使用模型实现扩展地图
- machine-learning - 如何使用 lstm 执行 TEXT_CLASSIFICATION 多类多输出分类
- html - webp/jpg - 仅在 html 文件中预加载所需的类型
- python - 如何使用来自 GitHub 的非打包 Python 代码
- android - 崩溃 - 签名 apk /bundle 但正常调试 apk 工作正常
- pandas - 如何使用 groupby 创建具有 MultiIndex 列的 DataFrame 的子图
- c# - 如何在 Blazor 服务器应用程序中进行多个异步调用?
- python - python用其他文件替换列值
- vim - 打开文件时如何运行 Vim 命令
- c++ - 如何将 lambda 传递给模板参数