Advertisement
butoff

Knigt's tour Iterative

Mar 30th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Program
  7. {
  8.     private static int[,] matrix;
  9.     private static int size;
  10.     private static int counter = 1;
  11.    
  12.     static int[] r = { 1, -1, 2, 1, -1, -2, 2, -2 };
  13.     static int[] c = { 2, 2, 1, -2, -2, 1, -1, -1 };
  14.  
  15.     static void Main()
  16.     {
  17.         size = int.Parse(Console.ReadLine());
  18.  
  19.         matrix = new int[size, size];
  20.  
  21.         InitMatrix();
  22.  
  23.         TraverseMatrix(0, 0);
  24.  
  25.         PrintMatrix();
  26.     }
  27.  
  28.     private static void TraverseMatrix(int row, int col)
  29.     {
  30.         matrix[row, col] = counter;
  31.  
  32.         Cell next = NextCell(row, col);
  33.  
  34.         while (next != null)
  35.         {
  36.             counter++;
  37.  
  38.             row = next.Row;
  39.  
  40.             col = next.Col;
  41.  
  42.             matrix[row, col] = counter;
  43.  
  44.             next = NextCell(row, col);
  45.         }
  46.     }
  47.  
  48.     private static Cell NextCell(int row, int col)
  49.     {
  50.         List<Cell> nextCells = new List<Cell>();
  51.  
  52.         for (int i = 0; i < 8; i++)
  53.         {
  54.             int currentRow = row + r[i];
  55.             int currentCol = col + c[i];
  56.  
  57.             if (IsEmpty(currentRow, currentCol))
  58.             {
  59.                 int moves = MovesAvailable(currentRow, currentCol);
  60.                 Cell cell = new Cell(currentRow, currentCol, moves);
  61.                 nextCells.Add(cell);
  62.             }
  63.         }
  64.  
  65.         if (nextCells.Count > 0)
  66.         {
  67.             return nextCells.OrderBy(c => c.Moves).First();
  68.         }
  69.         return null;
  70.     }
  71.  
  72.     private static int MovesAvailable(int row, int col)
  73.     {
  74.         int moves = 0;
  75.  
  76.         for (int i = 0; i < 8; i++)
  77.         {
  78.             if (IsEmpty(row + r[i], col + c[i]))
  79.             {
  80.                 moves++;
  81.             }
  82.         }  
  83.        
  84.         return moves;
  85.     }
  86.  
  87.     private static bool IsInside(int row, int col)
  88.     {
  89.         if (row < 0 || row >= size)
  90.         {
  91.             return false;
  92.         }
  93.         if (col < 0 || col >= size)
  94.         {
  95.             return false;
  96.         }
  97.  
  98.         return true;
  99.     }
  100.  
  101.     private static bool IsEmpty(int row, int col)
  102.     {
  103.         if (IsInside(row, col) && matrix[row, col] == 0)
  104.         {
  105.             return true;
  106.         }
  107.         return false;
  108.     }
  109.  
  110.     private static void InitMatrix()
  111.     {
  112.         for (int r = 0; r < size; r++)
  113.         {
  114.             for (int c = 0; c < size; c++)
  115.             {
  116.                 matrix[r, c] = 0;
  117.             }
  118.         }
  119.     }
  120.  
  121.     private static void PrintMatrix()
  122.     {
  123.         for (int r = 0; r < size; r++)
  124.         {
  125.             for (int c = 0; c < size; c++)
  126.             {
  127.                 string cell = matrix[r, c].ToString();
  128.                 Console.Write(cell.PadLeft(4, ' '));
  129.             }
  130.             Console.WriteLine();
  131.         }
  132.     }
  133.  
  134. }
  135.  
  136. public class Cell
  137. {
  138.     public Cell(int row, int col, int moves)
  139.     {
  140.         Row = row;
  141.         Col = col;
  142.         Moves = moves;
  143.     }
  144.  
  145.     public int Row { get; }
  146.     public int Col { get; }
  147.     public int Moves { get; }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement