Advertisement
DarkPotatoKing

solutions.py

Apr 3rd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. import copy
  2.  
  3. solution_count = 0
  4.  
  5. def dfs(start, path = []):
  6.     if start in path:
  7.         return
  8.  
  9.     path.append(start)
  10.     if len(path) == 9:
  11.         print path
  12.         global solution_count
  13.         solution_count += 1
  14.         return
  15.  
  16.     for i in adj_list[start]:
  17.         dfs(i, copy.deepcopy(path))
  18.  
  19. row = [0, 1, 1, 1, 2, 2, 2, 3, 3, 3]
  20. col = [0, 1, 2, 3, 1, 2, 3, 1, 2, 3]
  21.  
  22. grid = [[0, 0, 0, 0],
  23.         [0, 1, 2, 3],
  24.         [0, 4, 5, 6],
  25.         [0, 7, 8, 9]]
  26.  
  27. adj_list = list()
  28. for i in xrange(10):
  29.     adj_list.append(list())
  30.  
  31. valid_movement = [[1, 1], [1, -1], [-1, 1], [-1, -1], [1, 2], [1,-2], [-1, 2], [-1,-2], [2, 1], [2, -1], [-2, 1], [-2, -1]]
  32. rm = 0
  33. cm = 1
  34.  
  35. for i in xrange(1, 10):
  36.     for move in valid_movement:
  37.         r = row[i] + move[rm]
  38.         c = col[i] + move[cm]
  39.         if 1 <= r <= 3 and 1 <= c <= 3:
  40.             adj_list[i].append(grid[r][c])
  41.  
  42. for i in adj_list:
  43.     i.sort()
  44.  
  45. for x in xrange(1,10):
  46.     dfs(x, [])
  47. print solution_count, 'solutions'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement