#!/bin/python3 class Grid: def __init__(self, n, a, b): self.n = n self.a = a self.b = b self.grid = [[-1 for _ in range(n)] for _ in range(n)] self.iteration = 0 self.possible_positions = [(0, 0)] self.grid[0][0] = 0 self.result = None def iterate(self): self.iteration += 1 next_positions = [] for x, y in self.possible_positions: possible_next_positions = self.jump(x, y) for i, j in possible_next_positions: if i == self.n - 1 and j == self.n - 1: self.result = self.iteration return if self.grid[i][j] == -1: self.grid[i][j] = self.iteration next_positions.append((i, j)) if not next_positions: self.result = -1 return self.possible_positions = next_positions def solve(self): while self.result is None: self.iterate() return self.result def jump(self, x, y): a, b = self.a, self.b potential = [(x - a, y - b), (x - a, y + b), (x + a, y - b), (x + a, y + b), (x - b, y - a), (x - b, y + a), (x + b, y - a), (x + b, y + a)] return [(i, j) for i, j in potential if 0 <= i < self.n and 0 <= j < self.n] if __name__ == '__main__': n = int(input()) solution_matrix = [[-1 for _ in range(n-1)] for _ in range(n-1)] for a in range(1, n): for b in range(a, n): grid = Grid(n, a, b) solution = grid.solve() solution_matrix[a-1][b-1] = solution_matrix[b-1][a-1] = solution print('\n'.join(' '.join(str(i) for i in line) for line in solution_matrix))