using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); // your code goes here for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { Console.Write(NumMoves(n, i, j) + " "); } Console.WriteLine(""); } } static int NumMoves(int n, int a, int b) { List<Tuple<int,int>> moves = new List<Tuple<int,int>>(); moves.Add(new Tuple<int,int>(a,b)); moves.Add(new Tuple<int,int>(a,-b)); moves.Add(new Tuple<int,int>(-a,b)); moves.Add(new Tuple<int,int>(-a,-b)); if (a != b) { moves.Add(new Tuple<int,int>(b,a)); moves.Add(new Tuple<int,int>(b,-a)); moves.Add(new Tuple<int,int>(-b,a)); moves.Add(new Tuple<int,int>(-b,-a)); } int[,] board = new int[n,n]; Queue<int> queue = new Queue<int>(); queue.Enqueue(0); queue.Enqueue(0); queue.Enqueue(0); while (queue.Count > 0) { int r = queue.Dequeue(); int c = queue.Dequeue(); int cnt = queue.Dequeue(); if (r < 0 || r >= n || c < 0 || c >= n || board[r,c] != 0) continue; board[r,c] = cnt; foreach (Tuple<int,int> move in moves) { queue.Enqueue(r + move.Item1); queue.Enqueue(c + move.Item2); queue.Enqueue(cnt + 1); } } return board[n-1, n-1] > 0 ? board[n-1,n-1] : -1; } }