using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; //using Algorithms.Set_67; class Solution { private class Tokenizer { private string currentString = null; private string[] tokens = null; private int tokenNumber = 0; private static readonly char[] Separators = { ' ' }; public T NextToken(Func parser) { return parser(this.GetNextToken()); } public string NextToken() { return this.GetNextToken(); } public int NextInt() { return this.NextToken(int.Parse); } public long NextLong() { return this.NextToken(long.Parse); } private string GetNextToken() { if (this.currentString == null || this.tokenNumber == this.tokens.Length) { this.currentString = this.GetNextString(); while (this.currentString != null && this.currentString.Equals(string.Empty)) { this.currentString = this.GetNextString(); } if (this.currentString == null) { throw new Exception("End of input"); } this.tokens = this.currentString.Split(Separators, StringSplitOptions.RemoveEmptyEntries); this.tokenNumber = 0; } return this.tokens[this.tokenNumber++]; } private string GetNextString() { string content = Console.ReadLine(); if (content == null) { return null; } return content.Trim(); } } static void Main() { //Console.SetIn(new StreamReader(File.OpenRead("input.txt"))); //StreamWriter writer = new StreamWriter(File.Create("output.txt")); //Console.SetOut(writer); Tokenizer tokenizer = new Tokenizer(); int n = tokenizer.NextInt(); int maxStep = n - 1; int[,] result = new int[maxStep, maxStep]; for (int i = 0; i < maxStep; i++) { for (int j = i; j < maxStep; j++) { result[i, j] = result[j, i] = Knight.Solve(n, i + 1, j + 1); } } for (int i = 0; i < maxStep; i++) { for (int j = 0; j < maxStep; j++) { Console.Write(result[i, j] + " "); } Console.WriteLine(); } //writer.Close(); } class Knight { private static readonly int[] dx = { -1, -1, 1, 1 }; private static readonly int[] dy = { -1, 1, -1, 1 }; public static int Solve(int n, int a, int b) { int[,] steps = new int[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { steps[i, j] = int.MaxValue; } } steps[0, 0] = 0; Queue> queue = new Queue>(); queue.Enqueue(new Tuple(0, 0)); int x, y; while (queue.Count > 0) { var top = queue.Dequeue(); x = top.Item1; y = top.Item2; if (x == n - 1 && y == n - 1) { return steps[x, y]; } for (int i = 0; i < 4; i++) { Go(x + dx[i] * a, y + dy[i] * b, steps[x, y] + 1, steps, queue, n); Go(x + dx[i] * b, y + dy[i] * a, steps[x, y] + 1, steps, queue, n); } } return -1; } private static void Go(int nx, int ny, int nv, int[,] steps, Queue> queue, int n) { if (nx >= 0 && nx < n && ny >= 0 && ny < n && steps[nx, ny] > nv) { steps[nx, ny] = nv; queue.Enqueue(new Tuple(nx, ny)); } } } }