Advertisement
cyecize

Solve labyrinth problem with recursion

Mar 15th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace a07FindAllPathsInALabyrinth
  8. {
  9.     /**
  10.      *  Example input
  11.      *
  12.      *  3
  13.      *  3
  14.      *  -**-e
  15.      *  -----
  16.      *  *****
  17.      *
  18.      *  Where e is exit, * is wall.
  19.      *
  20.      *  Example output for the above input.
  21.      *  **Always starts at 0,0
  22.      *
  23.      *  DDRR
  24.      *  RRDD
  25.      */
  26.     class Program
  27.     {
  28.  
  29.         private static Labyrinth labyrinth;
  30.  
  31.         static void Main(string[] args)
  32.         {
  33.             labyrinth = ReadLabyrinth();
  34.             SolveLabyrinth(new Point(0, 0), new List<Point>(), new List<Direction>());
  35.         }
  36.  
  37.         private static void SolveLabyrinth(Point currentPoint, List<Point> previousPoints, List<Direction> directions)
  38.         {
  39.             if (previousPoints.Contains(currentPoint) || labyrinth.IsOutOfBounds(currentPoint))
  40.                 return;
  41.  
  42.             PointType pointType = labyrinth.GetPointType(currentPoint);
  43.  
  44.             if (pointType == PointType.WALL)
  45.                 return;
  46.  
  47.             if (pointType == PointType.EXIT)
  48.             {
  49.                 directions.ForEach(d => Console.Write(d.Name));
  50.                 Console.WriteLine();
  51.                 return;
  52.             }
  53.  
  54.             SolveLabyrinth(Direction.DOWN.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.DOWN });
  55.             SolveLabyrinth(Direction.UP.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.UP });
  56.             SolveLabyrinth(Direction.LEFT.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.LEFT });
  57.             SolveLabyrinth(Direction.RIGHT.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.RIGHT });
  58.         }
  59.  
  60.         private static Labyrinth ReadLabyrinth()
  61.         {
  62.             var rows = int.Parse(Console.ReadLine());
  63.             var cols = int.Parse(Console.ReadLine());
  64.  
  65.             var labyrinthMap = new PointType[rows][];
  66.  
  67.             for (var row = 0; row < rows; row++)
  68.             {
  69.                 labyrinthMap[row] = Console.ReadLine().ToCharArray().Take(cols).Select(PointType.ForName).ToArray();
  70.             }
  71.  
  72.             return new Labyrinth(labyrinthMap, rows, cols);
  73.         }
  74.     }
  75.  
  76.     internal class Labyrinth
  77.     {
  78.         private readonly PointType[][] labyrinth;
  79.  
  80.         public Labyrinth(PointType[][] labyrinth, int rows, int cols)
  81.         {
  82.             this.labyrinth = labyrinth;
  83.             this.RowCount = rows;
  84.             this.ColCount = cols;
  85.         }
  86.  
  87.         public int RowCount { get; }
  88.  
  89.         public int ColCount { get; }
  90.  
  91.         public bool IsOutOfBounds(Point point)
  92.         {
  93.             return point.Row < 0 || point.Row >= this.RowCount ||
  94.                    point.Col < 0 || point.Col >= this.ColCount;
  95.         }
  96.  
  97.         public PointType GetPointType(Point point)
  98.         {
  99.             return this.labyrinth[point.Row][point.Col];
  100.         }
  101.     }
  102. }
  103.  
  104. internal class Direction
  105. {
  106.     public static readonly Direction LEFT = new Direction("L", 0, -1);
  107.     public static readonly Direction RIGHT = new Direction("R", 0, 1);
  108.     public static readonly Direction UP = new Direction("U", -1, 0);
  109.     public static readonly Direction DOWN = new Direction("D", 1, 0);
  110.  
  111.     private readonly int rowDirection;
  112.  
  113.     private readonly int colDirection;
  114.  
  115.     private Direction(string name, int rowDirection, int colDirection)
  116.     {
  117.         this.Name = name;
  118.         this.rowDirection = rowDirection;
  119.         this.colDirection = colDirection;
  120.     }
  121.  
  122.     public string Name { get; set; }
  123.  
  124.     public Point GetNewPoint(Point point)
  125.     {
  126.         return new Point(point.Row + this.rowDirection, point.Col + this.colDirection);
  127.     }
  128.  
  129. }
  130.  
  131. internal class PointType
  132. {
  133.     public static readonly PointType WALL = new PointType('*');
  134.     public static readonly PointType EXIT = new PointType('e');
  135.     public static readonly PointType OTHER = new PointType('-');
  136.  
  137.     private PointType(char sign)
  138.     {
  139.         this.Sign = sign;
  140.     }
  141.  
  142.     public char Sign { get; set; }
  143.  
  144.     public static IEnumerable<PointType> Values
  145.     {
  146.         get
  147.         {
  148.             yield return WALL;
  149.             yield return EXIT;
  150.             yield return OTHER;
  151.         }
  152.     }
  153.  
  154.     public static PointType ForName(char pointName)
  155.     {
  156.         return Values.FirstOrDefault(pt => pt.Sign == pointName);
  157.     }
  158. }
  159.  
  160. internal class Point
  161. {
  162.     public Point()
  163.     {
  164.  
  165.     }
  166.  
  167.     public Point(int row, int col)
  168.     {
  169.         this.Row = row;
  170.         this.Col = col;
  171.     }
  172.  
  173.     public int Row { get; }
  174.  
  175.     public int Col { get; }
  176.  
  177.     public override bool Equals(object obj)
  178.     {
  179.         var otherPoint = (Point)obj;
  180.         return otherPoint.Row == this.Row && otherPoint.Col == this.Col;
  181.     }
  182.  
  183.     public override int GetHashCode()
  184.     {
  185.         return this.Row * 3000 + this.Col * 6035;
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement