这个推旗子机关太坑了,绞尽脑汁之后,还好我技高一筹
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


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
