#!/bin/python3 import sys def inBoardAndNotVisited(x, y, knight_visited, n): result = False if(x >= 0 and y >= 0 and x < n and y < n and not knight_visited[x][y]): result = True return result def knightNewReached(x_ini, y_ini, x_goal, y_goal, jump, knight_visited, next_pos, knight_new_reach, n): for i in range(8): x_n = x_ini + next_pos[i][0] y_n = y_ini + next_pos[i][1] if(inBoardAndNotVisited(x_n, y_n, knight_visited, n)): knight_visited[x_n][y_n] = True knight_new_reach.add((x_n, y_n)) def knightl(a, b, x_goal, y_goal, n): jump = 0 next_pos = [[a,b], [a,-b], [-a,b], [-a,-b], [b,a], [b,-a], [-b,a], [-b,-a] ] x_ini = 0 y_ini = 0 knight_visited = [[False for j in range(n)] for i in range(n)] knight_visited[x_ini][y_ini] = True knight_reach = {(x_ini, y_ini)} reached = False; #print("________________________________[", a, ",", b, "] START:(", x_ini, ",", y_ini, ")") while(not reached and len(knight_reach) > 0): knight_new_reach = set() for i in knight_reach: x_ini = i[0] y_ini = i[1] knightNewReached(x_ini, y_ini, x_goal, y_goal, jump, knight_visited, next_pos, knight_new_reach, n) knight_reach = knight_new_reach jump += 1 if (x_goal, y_goal) in knight_reach: reached = True if not reached: jump = -1 #print("FIND:(", a, ",", b, ")-", jump) return jump n = int(input().strip()) # your code goes here paths = [] knight_taken = [[False for j in range(n)] for i in range(n)] knight_value = [[0 for j in range(n)] for i in range(n)] for a in range(1, n, 1): line = [] for b in range(1, n, 1): if(not knight_taken[a][b]): jump = knightl(a, b, n-1, n-1, n) knight_taken[a][b] = True knight_taken[b][a] = True knight_value[a][b] = jump knight_value[b][a] = jump line.append(knight_value[a][b]) paths.append(line) for line in paths: print(line[0],end="") for i in range(1, len(line),1): print("",line[i], end="") print("")