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()); var knightL = new KnightL(n); knightL.PrintAllMovement(); } } class KnightL { private bool[,] _flags; private int[,] _moves; private int _size; private int _a; private int _b; private int[,] _globalMovements; public KnightL(int size) { _size = size; _flags = new bool[_size, _size]; _moves = new int[_size, _size]; _globalMovements = new int[_size, _size]; } public void PrintAllMovement() { for (var i = 1; i < _size; i++) { for (var j = 1; j < _size; j++) { int movement; if (j < i) { movement = _globalMovements[j, i]; } else { ClearMovements(); _a = i; _b = j; movement = GetMovement(); _globalMovements[i, j] = movement; } Console.Write(movement); if (j < _size - 1) Console.Write(" "); } Console.WriteLine(); } } public int GetMovement() { var first = new Box(0, 0); _flags[first.X, first.Y] = true; var queue = new Queue(); queue.Enqueue(first); while (queue.Count > 0) { var box = queue.Dequeue(); var neighbors = GetNeighbors(box).ToArray(); foreach (var neighbor in neighbors) { _flags[neighbor.X, neighbor.Y] = true; var isFinal = IsFinal(neighbor); if (isFinal) return box.Distance + 1; queue.Enqueue(neighbor); } } return -1; } private void ClearMovements() { for (int i = 0; i < _size; i++) { for (int j = 0; j < _size; j++) { _moves[i, j] = 0; _flags[i, j] = false; } } } private bool IsFinal(Box box) { return box.X == _size - 1 && box.Y == _size - 1; } private IEnumerable GetNeighbors(Box box) { var x1 = box.X - _a; var x2 = box.X + _a; var x3 = box.X - _b; var x4 = box.X + _b; var distance = box.Distance + 1; if (IsValid(x1)) { if (IsValid(box.Y - _b) && !IsVisited(x1, box.Y - _b)) yield return new Box(x1, box.Y - _b, distance); if (IsValid(box.Y + _b) && !IsVisited(x1, box.Y + _b)) yield return new Box(x1, box.Y + _b, distance); } if (IsValid(x2)) { if (IsValid(box.Y - _b) && !IsVisited(x2, box.Y - _b)) yield return new Box(x2, box.Y - _b, distance); if (IsValid(box.Y + _b) && !IsVisited(x2, box.Y + _b)) yield return new Box(x2, box.Y + _b, distance); } if (IsValid(x3)) { if (IsValid(box.Y - _a) && !IsVisited(x3, box.Y - _a)) yield return new Box(x3, box.Y - _a, distance); if (IsValid(box.Y + _a) && !IsVisited(x3, box.Y + _a)) yield return new Box(x3, box.Y + _a, distance); } if (IsValid(x4)) { if (IsValid(box.Y - _a) && !IsVisited(x4, box.Y - _a)) yield return new Box(x4, box.Y - _a, distance); if (IsValid(box.Y + _a) && !IsVisited(x4, box.Y + _a)) yield return new Box(x4, box.Y + _a, distance); } } private bool IsVisited(int i, int j) { return _flags[i, j]; } private bool IsValid(Box box) { return IsValid(box.X, box.Y); } private bool IsValid(int x, int y) { return IsValid(x) && IsValid(y); } private bool IsValid(int x) { return x >= 0 && x < _size; } } class Box { public int X { get; set; } public int Y { get; set; } public int Distance { get; set; } public Box(int x, int y) { X = x; Y = y; } public Box(int x, int y, int distance): this(x, y) { Distance = distance; } }