这个推旗子机关太坑了,绞尽脑汁之后,还好我技高一筹![](http://static.tieba.baidu.com/tb/editor/images/client/image_emoticon25.png)
python 代码我粘贴在这里了,改一下 init state 的初始状态,直接跑就可以了
from os import sys
import random
target = 2
step = 4
init_state = [1, 1, 0, 2]
def move(state, index, bias):
state[index] = (state[index] + bias) % step
def pull(state, index):
move(state, index, 1)
(index > 0) and move(state, index-1, -1)
((index + 1) < len(state)) and move(state, index+1, -1)
def check(state):
return all([s == target for s in state])
def apply_record(record):
state = [s for s in init_state]
print("init state: ", state)
for i, r in enumerate(record):
pull(state, r)
print("step:", i, "pull:", r, "=>", state)
def run(state, max_steps = 10, record = []):
global solution
if check(state):
apply_record(record)
return True
if max_steps == 0:
return False
index_list = list(range(len(init_state)))
random.shuffle(index_list)
for i in index_list:
new_state = [s for s in state]
record.append(i)
pull(new_state, i)
if run(new_state, max_steps-1):
return True
record.pop(-1)
for max_step in range(10, 20):
if run(init_state, max_steps=max_step):
break
![](http://tiebapic.baidu.com/forum/w%3D580/sign=40077e0d73d12f2ece05ae687fc0d5ff/7eed1d7b02087bf4313a4833b7d3572c13dfcf8a.jpg?tbpicau=2025-03-03-05_bd3f4e36bf816f87b20589557179b72e)
![](http://static.tieba.baidu.com/tb/editor/images/client/image_emoticon25.png)
python 代码我粘贴在这里了,改一下 init state 的初始状态,直接跑就可以了
from os import sys
import random
target = 2
step = 4
init_state = [1, 1, 0, 2]
def move(state, index, bias):
state[index] = (state[index] + bias) % step
def pull(state, index):
move(state, index, 1)
(index > 0) and move(state, index-1, -1)
((index + 1) < len(state)) and move(state, index+1, -1)
def check(state):
return all([s == target for s in state])
def apply_record(record):
state = [s for s in init_state]
print("init state: ", state)
for i, r in enumerate(record):
pull(state, r)
print("step:", i, "pull:", r, "=>", state)
def run(state, max_steps = 10, record = []):
global solution
if check(state):
apply_record(record)
return True
if max_steps == 0:
return False
index_list = list(range(len(init_state)))
random.shuffle(index_list)
for i in index_list:
new_state = [s for s in state]
record.append(i)
pull(new_state, i)
if run(new_state, max_steps-1):
return True
record.pop(-1)
for max_step in range(10, 20):
if run(init_state, max_steps=max_step):
break
![](http://tiebapic.baidu.com/forum/w%3D580/sign=40077e0d73d12f2ece05ae687fc0d5ff/7eed1d7b02087bf4313a4833b7d3572c13dfcf8a.jpg?tbpicau=2025-03-03-05_bd3f4e36bf816f87b20589557179b72e)