它应该做什么:将int作为输入,然后输出从1到n的所有数字的列表(或字符串)。
在那之后,我不知道如何用简单的语言解释这个,所以我只会告诉你。
所有合法举措(12345)= 21345,32145.43215,54321
我想要一个函数,当给定列表(或字符串)时,输出我可以播放的所有合法动作。我还希望为每个输出再次执行此操作。
所以,它应该是这样的:
所有合法举措(21345)= ...
所有合法举措(32145)= ...
所有合法举措(43215)= ...
所有合法举措(54321)= ...
深度k。我还没有想出深度的东西。还有很多其他的东西。
以下是代码:
#make the original list
n = int(input("Size of series: "))
s=[]
for i in range(n+1):
s.append(str(i))
del s[0]
print(s)
#take that and output all legal moves
print('\n' + 'All legal moves(' + ''.join(s) + '):')
b=[]
for i in range (n):
b.append(str(s[:i+1][::-1]+s[i+1:]))
del b[0]
b = ''.join(b)
print (b)
#i wanted to do the same thing here but something went wrong
print('\n' + 'All legal moves:')
a=[]
for i in range (n):
a.append(b[:i+1][::-1]+b[i+1:])
del a[0]
b = ','.join(b)
print(a)
这是n = 5的输出:
Size of series: 5
['1', '2', '3', '4', '5']
All legal moves(12345):
['2', '1', '3', '4', '5'],['3', '2', '1', '4', '5'],['4', '3', '2', '1',
'5'],['5', '4', '3', '2', '1']
All legal moves:
["'[2', '1', '3', '4', '5'],['3', '2', '1', '4', '5'],['4', '3', '2', '1',
'5'],['5', '4', '3', '2', '1']", "2'[', '1', '3', '4', '5'],['3', '2',
'1', '4', '5'],['4', '3', '2', '1', '5'],['5', '4', '3', '2', '1']",
"'2'[, '1', '3', '4', '5'],['3', '2', '1', '4', '5'],['4', '3', '2', '1',
'5'],['5', '4', '3', '2', '1']", ",'2'[ '1', '3', '4', '5'],['3', '2',
'1', '4', '5'],['4', '3', '2', '1', '5'],['5', '4', '3', '2', '1']"]
答案 0 :(得分:0)
系统地生成所有合法移动的一种方法是使用队列。为了进行处理,我们从队列的前面弹出游戏状态字符串,从该状态创建合法移动的列表,并将这些新状态附加到队列的末尾。标准库提供collections.deque
,这是此任务的理想选择。
我们还需要一套能够跟踪我们已经处理过的状态,以便我们不必再次处理它们。
在此代码中,我选择4作为大小以保持输出较小。因为maxsize == size
它产生全部4个! == 24个可能的字符串。
from collections import deque
def legal_moves(s):
return [s[i-1::-1] + s[i:] for i in range(2, len(s) + 1)]
size = 4
maxdepth = size
# Game states that we've already generated the moves for
seen = set()
# A queue of states that we haven't generated the moves for
states = deque()
# Build the starting state
start = ''.join([str(i) for i in range(1, size + 1)])
# and put it and its depth on the queue
states.append((start, 0))
# Process the states in the queue
print('current depth legal_moves')
while states:
# Get a state & depth from the queue
current_state, current_depth = states.popleft()
if current_state in seen:
continue
# Generate the legal moves for this state
moves = legal_moves(current_state)
print(current_state, current_depth, *moves)
seen.add(current_state)
# Put each of these states into the queue
# if it hasn't already been seen
next_depth = current_depth + 1
if next_depth > maxdepth:
continue
for state in moves:
if state not in seen:
states.append((state, next_depth))
<强>输出强>
current depth legal_moves
1234 0 2134 3214 4321
2134 1 1234 3124 4312
3214 1 2314 1234 4123
4321 1 3421 2341 1234
3124 2 1324 2134 4213
4312 2 3412 1342 2134
2314 2 3214 1324 4132
4123 2 1423 2143 3214
3421 2 4321 2431 1243
2341 2 3241 4321 1432
1324 3 3124 2314 4231
4213 3 2413 1243 3124
3412 3 4312 1432 2143
1342 3 3142 4312 2431
4132 3 1432 3142 2314
1423 3 4123 2413 3241
2143 3 1243 4123 3412
2431 3 4231 3421 1342
1243 3 2143 4213 3421
3241 3 2341 4231 1423
1432 3 4132 3412 2341
4231 4 2431 3241 1324
2413 4 4213 1423 3142
3142 4 1342 4132 2413