Advertisement
Filip13

horse movment in chess - backtracking

Apr 16th, 2024
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | Source Code | 0 0
  1. using System;
  2. namespace backtracking_konj_u_sahu
  3. {
  4.     internal class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             int tableDimension = 5;
  9.             int[,] table = new int[tableDimension, tableDimension];
  10.             int[,] moves = new int[,]{
  11.                 { -2, -1 }, { -2, 1 },  //left
  12.                 { -1, -2 }, { 1, -2 },  //up
  13.                 { 2,-1},{ 2,1},         //right
  14.                 { -1,2},{ 1,2}          //down
  15.             };
  16.  
  17.             //startingfrom (0,0)
  18.             //int posX = 0;
  19.             //int posY = 0;
  20.  
  21.             //starting from random position - from console
  22.  
  23.             int posX = int.Parse(Console.ReadLine());
  24.             int posY = int.Parse(Console.ReadLine());
  25.             int numMoves = 1;
  26.             table[posX, posY] = numMoves;
  27.  
  28.             GoThroughTable(table, moves, posX, posY, numMoves);
  29.         }
  30.  
  31.         private static void GoThroughTable(int[,] table, int[,] moves, int posX, int posY, int numMoves)
  32.         {
  33.             int tableSize = table.GetLength(0) * table.GetLength(1);
  34.             if (numMoves == tableSize)
  35.             {
  36.                 outputTable(table);
  37.                 return;
  38.             }
  39.  
  40.             for (int i = 0; i < moves.GetLength(0); i++)
  41.             {
  42.                 int newX = posX + moves[i, 0];
  43.                 int newY = posY + moves[i, 1];
  44.  
  45.                 if (ValidMove(newX, newY, table))
  46.                 {
  47.                     numMoves++;
  48.                     table[newX, newY] = numMoves;
  49.  
  50.                     GoThroughTable(table, moves, newX, newY, numMoves);
  51.  
  52.                     numMoves--;
  53.                     table[newX, newY] = 0;
  54.  
  55.                 }
  56.             }
  57.  
  58.  
  59.         }
  60.  
  61.         private static bool ValidMove(int newX, int newY, int[,] table)
  62.         {
  63.             return newX >= 0 && newY >= 0 && newX < table.GetLength(0) && newY < table.GetLength(0) && table[newX, newY] == 0;
  64.         }
  65.  
  66.         private static void outputTable(int[,] matrix)
  67.         {
  68.             for (int i = 0; i <= matrix.GetLength(0) - 1; i++)
  69.             {
  70.                 for (int j = 0; j <= matrix.GetLength(1) - 1; j++)
  71.                 {
  72.                     Console.Write($" {matrix[i, j],2}");
  73.                 }
  74.                 Console.WriteLine();
  75.             }
  76.  
  77.             Console.WriteLine();
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement