# import array n = 5 # розміри шахівниці dx = [2, 1,-1,-2,-2,-1, 1, 2] dy = [1, 2, 2, 1,-1,-2,-2,-1] h = n*n*[-1] # розв'язок def step (x,y,i): if (i == n*n): return True; for k in range(len(dx)): next_x = x + dx[k] next_y = y + dy[k] if ((next_x >= 0) and (next_x < n) and (next_y >= 0) and (next_y < n)): if (h[next_x*n+next_y] == -1): h[next_x*n + next_y] = i if step(next_x, next_y, i+1): return True else: h[next_x*n+next_y] = -1 return False h[0]=0 if (not step(0, 0, 1)): print("Розв'язків немає") else: print("Є щонайменше такий розв'язок:") for i in range(n): s="" for j in range(n): if (h[i*n+j]<10): s+=" " else: s+=" " s+=str(h[i*n+j]) print(s)