-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightL.py
More file actions
98 lines (35 loc) · 2.02 KB
/
KnightL.py
File metadata and controls
98 lines (35 loc) · 2.02 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
def initialize(n):
a = [[sys.maxsize for i in range(n)] for j in range(n)]
return a
def reachable(a,i,j,p_i,p_j):
if ((i+p_i>=0 and i+p_i <len(a)) and (j+p_j>=0 and j+p_j <len(a))):
return True
else:
return False
def search(a,prox_list):
queue = [(0,0,)]
a[0][0]=0
visited = [[False for i in range(len(a))] for j in range(len(a))]
while len(queue) > 0:
temp = queue[0]
del queue[0]
for i in range(len(prox_list)):
if (reachable(a,temp[0],temp[1],prox_list[i][0],prox_list[i][1])) and (visited[temp[0]+prox_list[i][0]][temp[1]+prox_list[i][1]]== False):
queue.append((temp[0]+prox_list[i][0],temp[1]+prox_list[i][1],))
if a[temp[0]+prox_list[i][0]][temp[1]+prox_list[i][1]] > a[temp[0]][temp[1]]+1:
a[temp[0]+prox_list[i][0]][temp[1]+prox_list[i][1]] = a[temp[0]][temp[1]]+1
visited[temp[0]+prox_list[i][0]][temp[1]+prox_list[i][1]] = True
res = a[len(a)-1][len(a)-1] if a[len(a)-1][len(a)-1]!=sys.maxsize else -1
return (res)
def KnightL(a,b,n):
prox_list = [[-1*a,-1*b],[-1*a,b],[a,-1*b],[a,b],[-1*b,-1*a],[-1*b,a],[b,-1*a],[b,a]]
mat = initialize(n)
return search(mat,prox_list)
if __name__=="__main__":
n = int(input())
for i in range(1,n):
for j in range(1,n):
print(KnightL(i,j,n),end = " ")
print("\n")