Skip to content

Instantly share code, notes, and snippets.

@haowen-xu
Created November 27, 2022 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haowen-xu/67fd8274d12814c75e918849d76d0c28 to your computer and use it in GitHub Desktop.
Save haowen-xu/67fd8274d12814c75e918849d76d0c28 to your computer and use it in GitHub Desktop.
from copy import copy
from collections import deque
"""
方向定义: 1 为正面,顺时针
3
2 4
1
方块编号:
a b c d e
联动定义:
"""
DIRECTION = 1 # 顺时针 = 1,逆时针 = 2
CUBES = {
'a': 3,
'b': 2,
'c': 3,
'd': 2,
}
LINKS = {
'a': 'ab',
'b': 'abc',
'c': 'bcd',
'd': 'cd',
}
def rotate(v):
v += DIRECTION
if v > 4:
v -= 4
if v < 1:
v += 1
return v
def search(path, cubes):
q = deque()
q.append((path, cubes))
while q:
path, cubes = q.popleft()
for c in CUBES:
path2 = copy(path)
path2.append(c)
cubes2 = copy(cubes)
cubes2[c] = rotate(cubes2[c])
for c2 in LINKS[c]:
if c != c2:
cubes2[c2] = rotate(cubes2[c2])
if all(v == 1 for v in cubes2.values()):
print(path2)
return
else:
q.append((path2, cubes2))
def main():
search([], CUBES)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment