Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace a07FindAllPathsInALabyrinth
- {
- /**
- * Example input
- *
- * 3
- * 3
- * -**-e
- * -----
- * *****
- *
- * Where e is exit, * is wall.
- *
- * Example output for the above input.
- * **Always starts at 0,0
- *
- * DDRR
- * RRDD
- */
- class Program
- {
- private static Labyrinth labyrinth;
- static void Main(string[] args)
- {
- labyrinth = ReadLabyrinth();
- SolveLabyrinth(new Point(0, 0), new List<Point>(), new List<Direction>());
- }
- private static void SolveLabyrinth(Point currentPoint, List<Point> previousPoints, List<Direction> directions)
- {
- if (previousPoints.Contains(currentPoint) || labyrinth.IsOutOfBounds(currentPoint))
- return;
- PointType pointType = labyrinth.GetPointType(currentPoint);
- if (pointType == PointType.WALL)
- return;
- if (pointType == PointType.EXIT)
- {
- directions.ForEach(d => Console.Write(d.Name));
- Console.WriteLine();
- return;
- }
- SolveLabyrinth(Direction.DOWN.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.DOWN });
- SolveLabyrinth(Direction.UP.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.UP });
- SolveLabyrinth(Direction.LEFT.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.LEFT });
- SolveLabyrinth(Direction.RIGHT.GetNewPoint(currentPoint), new List<Point>(previousPoints) { currentPoint }, new List<Direction>(directions) { Direction.RIGHT });
- }
- private static Labyrinth ReadLabyrinth()
- {
- var rows = int.Parse(Console.ReadLine());
- var cols = int.Parse(Console.ReadLine());
- var labyrinthMap = new PointType[rows][];
- for (var row = 0; row < rows; row++)
- {
- labyrinthMap[row] = Console.ReadLine().ToCharArray().Take(cols).Select(PointType.ForName).ToArray();
- }
- return new Labyrinth(labyrinthMap, rows, cols);
- }
- }
- internal class Labyrinth
- {
- private readonly PointType[][] labyrinth;
- public Labyrinth(PointType[][] labyrinth, int rows, int cols)
- {
- this.labyrinth = labyrinth;
- this.RowCount = rows;
- this.ColCount = cols;
- }
- public int RowCount { get; }
- public int ColCount { get; }
- public bool IsOutOfBounds(Point point)
- {
- return point.Row < 0 || point.Row >= this.RowCount ||
- point.Col < 0 || point.Col >= this.ColCount;
- }
- public PointType GetPointType(Point point)
- {
- return this.labyrinth[point.Row][point.Col];
- }
- }
- }
- internal class Direction
- {
- public static readonly Direction LEFT = new Direction("L", 0, -1);
- public static readonly Direction RIGHT = new Direction("R", 0, 1);
- public static readonly Direction UP = new Direction("U", -1, 0);
- public static readonly Direction DOWN = new Direction("D", 1, 0);
- private readonly int rowDirection;
- private readonly int colDirection;
- private Direction(string name, int rowDirection, int colDirection)
- {
- this.Name = name;
- this.rowDirection = rowDirection;
- this.colDirection = colDirection;
- }
- public string Name { get; set; }
- public Point GetNewPoint(Point point)
- {
- return new Point(point.Row + this.rowDirection, point.Col + this.colDirection);
- }
- }
- internal class PointType
- {
- public static readonly PointType WALL = new PointType('*');
- public static readonly PointType EXIT = new PointType('e');
- public static readonly PointType OTHER = new PointType('-');
- private PointType(char sign)
- {
- this.Sign = sign;
- }
- public char Sign { get; set; }
- public static IEnumerable<PointType> Values
- {
- get
- {
- yield return WALL;
- yield return EXIT;
- yield return OTHER;
- }
- }
- public static PointType ForName(char pointName)
- {
- return Values.FirstOrDefault(pt => pt.Sign == pointName);
- }
- }
- internal class Point
- {
- public Point()
- {
- }
- public Point(int row, int col)
- {
- this.Row = row;
- this.Col = col;
- }
- public int Row { get; }
- public int Col { get; }
- public override bool Equals(object obj)
- {
- var otherPoint = (Point)obj;
- return otherPoint.Row == this.Row && otherPoint.Col == this.Col;
- }
- public override int GetHashCode()
- {
- return this.Row * 3000 + this.Col * 6035;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement