Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def dfs(l, st, f, k, callback):
- l[st[0]][st[1]] = k
- if st == f and k == len(l) * 3:
- callback(l)
- l[st[0]][st[1]] = None
- return
- candidates = (
- (st[0] - 1, st[1]),
- (st[0] + 1, st[1]),
- (st[0], st[1] - 1),
- (st[0], st[1] + 1))
- for c in candidates:
- if c[0] < 0 or c[0] >= len(l) or c[1] < 0 or c[1] > 2 or l[c[0]][c[1]]:
- continue
- dfs(l, c, f, k+1, callback)
- l[st[0]][st[1]] = None
- def check(n):
- cnt = 0
- l = [[None] * 3 for _ in range(n)]
- def c(_):
- nonlocal cnt
- cnt += 1
- dfs(l, (0, 0), (n - 1, 0), 1, c)
- cnt_l = cnt
- cnt = 0
- dfs(l, (0, 0), (n - 1, 2), 1, c)
- cnt_d = cnt
- cnt = 0
- dfs(l, (0, 0), (0, 2), 1, c)
- cnt_s = cnt
- return cnt_d, cnt_l, cnt_s
- for i in range(1, 11):
- print(check(i))
- # Out:
- # (1, 0, 1)
- # (1, 1, 0)
- # (2, 2, 2)
- # (4, 4, 0)
- # (8, 8, 4)
- # (16, 16, 0)
- # (32, 32, 8)
- # (64, 64, 0)
- # (128, 128, 16)
- # (256, 256, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement