Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- solution_count = 0
- def dfs(start, path = []):
- if start in path:
- return
- path.append(start)
- if len(path) == 9:
- print path
- global solution_count
- solution_count += 1
- return
- for i in adj_list[start]:
- dfs(i, copy.deepcopy(path))
- row = [0, 1, 1, 1, 2, 2, 2, 3, 3, 3]
- col = [0, 1, 2, 3, 1, 2, 3, 1, 2, 3]
- grid = [[0, 0, 0, 0],
- [0, 1, 2, 3],
- [0, 4, 5, 6],
- [0, 7, 8, 9]]
- adj_list = list()
- for i in xrange(10):
- adj_list.append(list())
- 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]]
- rm = 0
- cm = 1
- for i in xrange(1, 10):
- for move in valid_movement:
- r = row[i] + move[rm]
- c = col[i] + move[cm]
- if 1 <= r <= 3 and 1 <= c <= 3:
- adj_list[i].append(grid[r][c])
- for i in adj_list:
- i.sort()
- for x in xrange(1,10):
- dfs(x, [])
- print solution_count, 'solutions'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement