Advertisement
vencinachev

MazeArray

Sep 5th, 2019 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2.  
  3. namespace BacktrackingMaze
  4. {
  5.     class Program
  6.     {
  7.  
  8.         static char[,] lab1 =
  9.         {
  10.             { ' ', ' ', ' ', ' ' },
  11.             { ' ', '*', ' ', ' ' },
  12.             { 'f', ' ', ' ', 'f' }
  13.         };
  14.  
  15.         static char[,] lab =
  16.         {
  17.             { ' ', ' ', ' ', '*', ' ', ' ', ' ' },
  18.             { '*', '*', ' ', '*', ' ', '*', ' ' },
  19.             { ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  20.             { ' ', '*', '*', '*', '*', '*', ' ' },
  21.             { ' ', ' ', ' ', ' ', ' ', ' ', 'f' },
  22.         };
  23.  
  24.         static void FindPath(int row, int col)
  25.         {
  26.             if (row < 0 || col < 0 || row >= lab.GetLength(0) || col >= lab.GetLength(1) || lab[row, col] == 'v')
  27.             {
  28.                 // we are out of labyrinth
  29.                 return;
  30.             }
  31.  
  32.             if (lab[row, col] == 'f')
  33.             {
  34.                 Console.WriteLine("Found path!");
  35.             }
  36.  
  37.             if (lab[row, col] == ' ')
  38.             {
  39.                 lab[row, col] = 'v';
  40.                 FindPath(row, col - 1);
  41.                 FindPath(row - 1, col);
  42.                 FindPath(row, col + 1);
  43.                 FindPath(row + 1, col);
  44.                 lab[row, col] = ' ';
  45.             }
  46.         }
  47.  
  48.  
  49.         static char[] path = new char[lab.GetLength(0) * lab.GetLength(1)];
  50.         static int position = 0;
  51.         static void FindPath(int row, int col, char direction)
  52.         {
  53.             // out of lab
  54.             if((col < 0) || (row < 0) || (col >= lab.GetLength(1)) || (row >= lab.GetLength(0)))
  55.             {
  56.                 return;
  57.             }
  58.  
  59.             path[position] = direction;
  60.             position++;
  61.  
  62.             if (lab[row, col] == 'f')
  63.             {
  64.                 PrintPath(path, 0, position - 1);
  65.             }
  66.  
  67.             if (lab[row, col] == ' ')
  68.             {
  69.                 lab[row, col] = 's';
  70.  
  71.                 FindPath(row, col - 1, 'L');
  72.                 FindPath(row - 1, col, 'U');
  73.                 FindPath(row, col + 1, 'R');
  74.                 FindPath(row + 1, col, 'D');
  75.  
  76.                 // mark as free
  77.                 lab[row, col] = ' ';
  78.             }
  79.             position--;
  80.         }
  81.  
  82.         static void PrintPath(char[] path, int startPos, int endPos)
  83.         {
  84.             Console.Write("Found path to the exit: ");
  85.             for (int pos = startPos; pos <= endPos; pos++)
  86.             {
  87.                 Console.Write(path[pos]);
  88.             }
  89.             Console.WriteLine();
  90.         }
  91.  
  92.         static void Main(string[] args)
  93.         {
  94.             FindPath(0, 0);
  95.             //FindPath(0, 0, 'S');
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement