SHARE
TWEET

SnakeWay checker

a guest Dec 5th, 2019 11 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  def dfs(l, st, f, k, callback):
  2.      l[st[0]][st[1]] = k
  3.      if st == f and k == len(l) * 3:
  4.          callback(l)
  5.          l[st[0]][st[1]] = None
  6.          return
  7.      candidates = (
  8.      (st[0] - 1, st[1]),
  9.      (st[0] + 1, st[1]),
  10.      (st[0], st[1] - 1),
  11.      (st[0], st[1] + 1))
  12.      for c in candidates:
  13.          if c[0] < 0 or c[0] >= len(l) or c[1] < 0 or c[1] > 2 or l[c[0]][c[1]]:
  14.              continue
  15.          dfs(l, c, f, k+1, callback)
  16.      l[st[0]][st[1]] = None
  17.  
  18. def check(n):
  19.     cnt = 0
  20.     l = [[None] * 3 for _ in range(n)]
  21.     def c(_):
  22.         nonlocal cnt
  23.         cnt += 1
  24.     dfs(l, (0, 0), (n - 1, 0), 1, c)
  25.     cnt_l = cnt
  26.     cnt = 0
  27.     dfs(l, (0, 0), (n - 1, 2), 1, c)
  28.     cnt_d = cnt
  29.     cnt = 0
  30.     dfs(l, (0, 0), (0, 2), 1, c)
  31.     cnt_s = cnt
  32.     return cnt_d, cnt_l, cnt_s
  33.  
  34. for i in range(1, 11):
  35.     print(check(i))
  36. # Out:
  37. # (1, 0, 1)
  38. # (1, 1, 0)
  39. # (2, 2, 2)
  40. # (4, 4, 0)
  41. # (8, 8, 4)
  42. # (16, 16, 0)
  43. # (32, 32, 8)
  44. # (64, 64, 0)
  45. # (128, 128, 16)
  46. # (256, 256, 0)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top