Advertisement
Filip13

maze - backtracking

Apr 16th, 2024
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | Source Code | 0 0
  1. namespace prolazak_kroz_lavirint___backtracking
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             string[,] maze = new string[,]
  8.             {
  9.                 { "", "X", "X", "X", "X" },
  10.                 { "", "X",  "",  "",  "" },
  11.                 { "",  "",  "",  "",  "" },
  12.                 { "", "X",  "", "X",  "" },
  13.             };
  14.  
  15.             int xStart = 1;
  16.             int yStart = 0;
  17.  
  18.             int xEnd = 2;
  19.             int yEnd = 4;
  20.  
  21.  
  22.             int[,] moves = new int[,]
  23.             {
  24.                 {-1, 0 },   //left
  25.                 {0, -1 },   //up
  26.                 {1, 0 },    //right
  27.                 {0, 1 },    //down
  28.             };
  29.  
  30.             maze[xStart, yStart] = ".";
  31.  
  32.             bool gotOut = false;
  33.  
  34.             //GetOutOfTheMaze(maze, moves, xStart, yStart, xEnd, yEnd, ref gotOut);//first solution, not fastest
  35.  
  36.             int numSteps = 0;
  37.             int minSteps = int.MaxValue;
  38.             //string[,] shortestSolution = new string[,];
  39.  
  40.             GetOutOfTheMazeShortest(maze, moves, xStart, yStart, xEnd, yEnd, numSteps, ref minSteps);
  41.         }
  42.  
  43.         private static void GetOutOfTheMaze(string[,] maze, int[,] moves, int x, int y, int xEnd, int yEnd, ref bool gotOut)
  44.         {
  45.             if (x == xEnd && y == yEnd)
  46.             {
  47.                 outputMatrix(maze);
  48.                 gotOut = true;
  49.                 return;
  50.             }
  51.  
  52.  
  53.             for (int i = 0; i <= moves.GetLength(0) - 1; i++)
  54.             {
  55.                 int newX = x + moves[i, 0];
  56.                 int newY = y + moves[i, 1];
  57.  
  58.  
  59.                 if (ValidMove(newX, newY, maze) && !gotOut)
  60.                 {
  61.                     maze[newX, newY] = ".";//add new move
  62.  
  63.                     GetOutOfTheMaze(maze, moves, newX, newY, xEnd, yEnd, ref gotOut);
  64.  
  65.                     maze[newX, newY] = "";//erase that(new) move
  66.  
  67.                 }
  68.             }
  69.  
  70.  
  71.         }
  72.  
  73.         private static void GetOutOfTheMazeShortest(string[,] maze, int[,] moves, int x, int y, int xEnd, int yEnd, int numSteps, ref int minSteps)
  74.         {
  75.             if (x == xEnd && y == yEnd)
  76.             {
  77.                 if (minSteps < numSteps)
  78.                 {
  79.                     outputMatrix(maze);
  80.                     minSteps = numSteps;
  81.                 }
  82.  
  83.                 return;
  84.             }
  85.  
  86.  
  87.             for (int i = 0; i <= moves.GetLength(0) - 1; i++)
  88.             {
  89.                 int newX = x + moves[i, 0];
  90.                 int newY = y + moves[i, 1];
  91.  
  92.  
  93.                 if (ValidMove(newX, newY, maze))
  94.                 {
  95.                     maze[newX, newY] = ".";//add new move
  96.  
  97.                     GetOutOfTheMazeShortest(maze, moves, newX, newY, xEnd, yEnd);
  98.  
  99.                     maze[newX, newY] = "";//erase that(new) move
  100.  
  101.                 }
  102.             }
  103.  
  104.  
  105.         }
  106.  
  107.  
  108.         private static bool ValidMove(int newX, int newY, string[,] maze)
  109.         {
  110.             return newX >= 0 && newX <= maze.GetLength(0) - 1 && newY >= 0 && newY <= maze.GetLength(1) - 1 && maze[newX, newY] == "";
  111.         }
  112.  
  113.         private static void outputMatrix(string[,] maze)
  114.         {
  115.             for (int i = 0; i <= maze.GetLength(0) - 1; i++)
  116.             {
  117.                 for (int j = 0; j <= maze.GetLength(1) - 1; j++)
  118.                 {
  119.                     Console.Write(maze[i, j].PadLeft(2));
  120.                 }
  121.                 Console.WriteLine();
  122.             }
  123.  
  124.             Console.WriteLine();
  125.         }
  126.  
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement