#!/bin/python3 import sys n = int(input().strip()) # your code goes here def isValid(pos): return pos[0] >= 0 and pos[1] >= 0 and pos[0] < n and pos[1] < n def possible_moves(position,i,j): x, y = [int(x) for x in position.split(',')] poss_moves = [(x+i, y+j), (x-i, y+j), (x+i, y-j), (x-i, y-j), (x+j, y+i), (x-j, y+i), (x+j, y-i), (x-j, y-i)] out_moves = [] for move in poss_moves: if isValid(move): out_moves.append({'position': '%s,%s'%(move[0],move[1])}) return out_moves def bfs(root, goal, i, j): node_set = [] queue = [] root['parent'] = None queue.append(root) while len(queue) > 0: current = queue.pop(0) if current['position'] == goal['position']: return current for node in possible_moves(current['position'],i,j): if not any([node['position'] == x['position'] for x in node_set]): node_set.append(node) node['parent'] = current queue.append(node) return False def knightL(i,j): if True: root = {} root['position'] = '%s,%s'%(0,0) goal = {} goal['position'] = '%s,%s'%(n-1,n-1) success = bfs(root, goal, i, j) if success: moves = 0 while success.get('parent', False): moves +=1 success = success['parent'] return moves else: return -1 return '-1' output = '' for i in range(1,n): for j in range(1,n): output += str(knightL(i,j)) + ' ' output += '\n' print(output)