예제 #1
0
        private static List<Position> GetPossibleDirections(Position position, Labyrinth labyrinth)
        {
            var possibleDirections = new List<Position>();
            var left = position.Left;
            var right = position.Right;
            var down = position.Down;
            var up = position.Up;

            if (labyrinth.CanMoveTo(left))
            {
                possibleDirections.Add(left);
            }
            if (labyrinth.CanMoveTo(right))
            {
                possibleDirections.Add(right);
            }
            if (labyrinth.CanMoveTo(down))
            {
                possibleDirections.Add(down);
            }
            if (labyrinth.CanMoveTo(up))
            {
                possibleDirections.Add(up);
            }

            return possibleDirections;
        }
예제 #2
0
        private static void GetAllPaths(Position position, Position target, Labyrinth labyrinth)
        {
            labyrinth.Visit(position);

            if (position == target)
            {
                foundPaths.Add(labyrinth);
                return;
            }

            var possibleDirections = new List<Position>
            {
                position.Left,
                position.Right,
                position.Down,
                position.Up
            };

            foreach (var direction in possibleDirections)
            {
                if (labyrinth.CanMoveTo(direction))
                {
                    var newLabyrinth = new Labyrinth(labyrinth.Matrix, new HashSet<Position>(labyrinth.Visited));
                    GetAllPaths(direction, target, newLabyrinth);
                }
            }
        }
예제 #3
0
        static void Main()
        {
            var startPosition = new Position(0, 0);
            var targetPosition = new Position(5, 5);

            Console.WriteLine("Start position: {0}", startPosition);
            Console.WriteLine("Start position: {0}", targetPosition);

            var matrix = new char[6, 6]
            {
                {'0','0','0','x','0','x'},
                {'0','x','0','0','0','x'},
                {'0','0','x','x','0','0'},
                {'0','x','0','0','0','0'},
                {'0','0','0','x','x','0'},
                {'0','0','0','x','x','0'}
            };

            var matrixReloaded = new char[6, 6]
            {
                {'0','0','0','0','0','0'},
                {'0','x','0','x','0','x'},
                {'0','x','0','x','0','x'},
                {'0','x','0','x','0','x'},
                {'0','x','0','x','0','x'},
                {'0','0','0','0','0','0'}
            };

            var labirynth = new Labyrinth(matrixReloaded);

            Console.WriteLine("Labyrinth:");
            Console.WriteLine(labirynth);

            GetAllPaths(startPosition, targetPosition, labirynth);

            foreach (var path in foundPaths)
            {
                path.PrintVisited();
                Console.WriteLine();
            }
        }
예제 #4
0
        private static void PathAvailable(Position position, Position target, Labyrinth labyrinth)
        {
            if (position == target)
            {
                pathFound = true;
            }

            labyrinth.Visit(position);

            var possibleDirections = GetPossibleDirections(position, labyrinth);

            foreach (var direction in possibleDirections)
            {
                if (labyrinth.CanMoveTo(direction) && !pathFound)
                {
                   PathAvailable(direction, target, labyrinth);
                }
            }
        }
예제 #5
0
        static void Main()
        {
            var matrixA = new char[6, 6]
            {
                {'0','0','0','x','0','x'},
                {'0','x','0','0','0','x'},
                {'0','0','x','x','0','0'},
                {'0','x','0','0','0','0'},
                {'0','0','0','x','x','0'},
                {'0','0','0','x','x','0'}
            };

            var matrixB = new char[6, 6]
            {
                {'0','0','0','0','0','0'},
                {'0','x','0','x','0','x'},
                {'0','x','0','x','0','x'},
                {'0','x','0','x','x','0'},
                {'0','x','0','x','0','0'},
                {'0','0','0','x','0','0'}
            };

            var matrixC = CreateEmptyMatrix(92, 92);

            var matrix = matrixB; // Change the matrix

            var labirynth = new Labyrinth(matrix);
            var startPosition = new Position(0, 0);
            var targetPosition = new Position(matrix.GetLength(0) - 1, matrix.GetLength(1) - 1);

            PathAvailable(startPosition, targetPosition, labirynth);

            Console.WriteLine("Path available: {0}", pathFound);
        }