#!/bin/python import sys def next_board(n, a, b): positions = [(0, 0)] board = [[0 for _ in range(n)] for __ in range (n)] board[0][0] = 1 moves_left = True steps = 1 while moves_left: steps += 1 next_positions = [] moves_left = False for position in positions: for next_pos in [(a, b), (-a, -b), (a, -b), (-a, b), (b, a), (-b, -a), (b, -a), (-b, a)]: next_x = position[0] + next_pos[0] next_y = position[1] + next_pos[1] if 0 <= next_x < n and 0 <= next_y < n and board[next_x][next_y] == 0: if next_x == n - 1 and next_y == n - 1: return steps - 1 board[next_x][next_y] = steps # print board next_positions.append((next_x, next_y)) moves_left = True positions = next_positions return -1 def main(n): result = [[0 for _ in range(n - 1)] for __ in range (n - 1)] for i in range(1, n): for j in range(1, n): result[i - 1][j -1] = str(next_board(n, i, j)) for r in result: print ' '.join(r) n = int(raw_input().strip()) main(n) # your code goes here