Advertisement
gabi11

Algorithms - 7. Paths in Labyrinth

Sep 6th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Paths_in_Labyrinth
  6. {
  7.     class Program
  8.     {
  9.         static char[,] labyrinth;
  10.  
  11.         static List<char> path = new List<char>();
  12.  
  13.         static void ReadLabyrinth()
  14.         {
  15.             int rows = int.Parse(Console.ReadLine());
  16.             int cols = int.Parse(Console.ReadLine());
  17.  
  18.             labyrinth = new char[rows, cols];
  19.  
  20.             for (int row = 0; row < rows; row++)
  21.             {
  22.                 string currentLine = Console.ReadLine();
  23.  
  24.                 for (int col = 0; col < cols; col++)
  25.                 {
  26.                     labyrinth[row, col] = currentLine[col];
  27.                 }
  28.             }
  29.         }
  30.  
  31.         static void Solve(int row, int col, char direction)
  32.         {
  33.             if (OutsideOfLabyrinth(row, col))
  34.             {
  35.                 return;
  36.             }
  37.  
  38.             path.Add(direction);
  39.  
  40.             if (IsExit(row, col))
  41.             {
  42.                 PrintSolution();
  43.             }
  44.  
  45.             else if (IsPassable(row, col))
  46.             {
  47.                 labyrinth[row, col] = 'x';
  48.  
  49.                 Solve(row + 1, col, 'D'); // down
  50.                 Solve(row - 1, col, 'U'); // up
  51.                 Solve(row, col + 1, 'R'); // right
  52.                 Solve(row, col - 1, 'L'); // left
  53.  
  54.                 labyrinth[row, col] = '-';
  55.             }
  56.  
  57.             path.RemoveAt(path.Count - 1);
  58.         }
  59.  
  60.         private static bool IsPassable(int row, int col)
  61.         {
  62.             // already visited or wall
  63.             if (labyrinth[row, col] == 'x' || labyrinth [row, col] == '*')
  64.             {
  65.                 return false;
  66.             }
  67.  
  68.             return true;
  69.         }
  70.  
  71.         private static void PrintSolution()
  72.         {
  73.             Console.WriteLine(string.Join(string.Empty, path.Skip(1)));
  74.         }
  75.  
  76.         private static bool IsExit(int row, int col)
  77.         {
  78.             return labyrinth[row, col] == 'e';
  79.         }
  80.  
  81.         private static bool OutsideOfLabyrinth(int row, int col)
  82.         {
  83.             if (row < 0 || row >= labyrinth.GetLength(0))
  84.             {
  85.                 return true;
  86.             }
  87.  
  88.             if (col < 0 || col >= labyrinth.GetLength(1))
  89.             {
  90.                 return true;
  91.             }
  92.            
  93.             return false;
  94.         }
  95.  
  96.         static void Main(string[] args)
  97.         {
  98.             ReadLabyrinth();
  99.             Solve(0, 0, 'N');
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement