forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.py
38 lines (30 loc) · 1.03 KB
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def allowed_moves(start, visited, N):
x, y = start//N, start%N
moves = [(x+2, y+1), (x+2, y-1), \
(x+1, y+2), (x+1, y-2), \
(x-1, y+2), (x-1, y-2), \
(x-2, y+1), (x-2, y-1)]
return [x*N + y for (x, y) in moves if x>=0 and y>=0 and x<N and y<N and \
x*N + y not in visited]
def countTours(start, visited, N):
if len(visited)==N**2:
return 1
next_moves = allowed_moves(start, visited, N)
count = 0
for move in next_moves:
visited_tmp = visited.copy()
visited_tmp.add(move)
count += countTours(move, visited_tmp, N)
return count
def countKnightsTours(N):
num_cells = N**2
all_tours = 0
for start in range(num_cells):
visited = {start}
num_tours = countTours(start, visited, N)
print({start: num_tours})
all_tours += num_tours
return all_tours
if __name__=='__main__':
for i in range(1, 6):
print('For N = {} Number of Knights Tours = {}'.format(i, countKnightsTours(i)))