示例#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
        public char this[Position position]
        {
            get
            {
                return this.matrix[position.Row, position.Col];
            }

            private set
            {
                this.matrix[position.Row, position.Col] = value;
            }
        }
示例#4
0
        public bool CanMoveTo(Position position)
        {
            if (position.Row < 0 || position.Row >= this.matrix.GetLength(0))
            {
                return false;
            }

            if (position.Col < 0 || position.Col >= this.matrix.GetLength(1))
            {
                return false;
            }

            if (this[position] != '0')
            {
                return false;
            }

            return true;
        }
示例#5
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();
            }
        }
示例#6
0
 private bool IsVisited(Position position)
 {
     var isVisited = this.visited.Contains<Position>(position);
     return isVisited;
 }
示例#7
0
 public void Visit(Position position)
 {
     this.visited.Add(position);
 }
示例#8
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);
                }
            }
        }
示例#9
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);
        }
示例#10
0
 public void Visit(Position position)
 {
     this[position] = 'v';
 }