/* * KnightL on a Chessboard * by Joel Enriquez * 2017/02/12 */ using System; using System.Collections.Generic; class Solution { static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); for (int a = 1; a < n; a++) { for (int b = 1; b < n; b++) { int[][] board = new int[n][]; for (int i = 0; i < n; i++) { board[i] = new int[n]; } int x1 = 0, y1 = 0, x2 = 0, y2 = 0; Queue> queue = new Queue>(); queue.Enqueue(new Tuple(x1, y1)); Queue map = new Queue(); map.Enqueue(1); bool reached = false; board[0][0] = 1; while (queue.Count > 0 && !reached) { Tuple p = queue.Dequeue(); int step = map.Dequeue() + 1; x1 = p.Item1; y1 = p.Item2; x2 = x1 + a; y2 = y1 + b; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } x2 = x1 + a; y2 = y1 - b; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } x2 = x1 - a; y2 = y1 + b; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } x2 = x1 - a; y2 = y1 - b; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } x2 = x1 + b; y2 = y1 + a; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } x2 = x1 + b; y2 = y1 - a; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } x2 = x1 - b; y2 = y1 + a; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } x2 = x1 - b; y2 = y1 - a; if (SetBoard(board, x2, y2, n, step, out reached)) { queue.Enqueue(new Tuple(x2, y2)); map.Enqueue(step); } } Console.Write("{0} ", board[n - 1][n - 1] - 1); } Console.WriteLine(); } Console.ReadLine(); } static bool SetBoard(int[][] board, int x, int y, int n, int step, out bool reached) { reached = false; if (x + 1 == n && y + 1 == n) { reached = true; } if (-1 < x && x < n && -1 < y && y < n && board[y][x] == 0) { board[y][x] = step; return true; } return false; } }