Пример #1
0
        private List <MazeCell> FindLongestPath(MazeCell currentCell, MazeCell exitCell, MazeCell[,] mazeArray)
        {
            List <MazeCell> longestPath = null;

            if (currentCell.Y < 0 || currentCell.X < 0)
            {
                return(null);
            }
            if (currentCell.Y == mazeArray.GetLength(0) || currentCell.X == mazeArray.GetLength(1))
            {
                return(null);
            }
            if (mazeArray[currentCell.Y, currentCell.X].IsVisited == true)
            {
                return(null);
            }
            if (currentCell.Character == exitCell.Character)
            {
                var longPath = new List <MazeCell>();
                longPath.Add(currentCell);
                return(longPath);
            }

            mazeArray[currentCell.Y, currentCell.X].IsVisited = true;
            var nextCells = new List <MazeCell>();

            if (mazeArray[currentCell.Y - 1, currentCell.X].IsVisited == false)
            {
                nextCells.Add(mazeArray[currentCell.Y - 1, currentCell.X]);
            }
            if (mazeArray[currentCell.Y + 1, currentCell.X].IsVisited == false)
            {
                nextCells.Add(mazeArray[currentCell.Y + 1, currentCell.X]);
            }
            if (mazeArray[currentCell.Y, currentCell.X - 1].IsVisited == false)
            {
                nextCells.Add(mazeArray[currentCell.Y, currentCell.X - 1]);
            }
            if (mazeArray[currentCell.Y, currentCell.X + 1].IsVisited == false)
            {
                nextCells.Add(mazeArray[currentCell.Y, currentCell.X + 1]);
            }

            var maxLength = -1;

            foreach (var nextCell in nextCells)
            {
                var longPath = FindLongestPath(nextCell, exitCell, mazeArray);
                if (longPath != null && longPath.Count > maxLength)
                {
                    maxLength = longPath.Count;
                    longPath.Add(currentCell);
                    longestPath = longPath;
                }
            }
            mazeArray[currentCell.Y, currentCell.X].IsVisited = false;
            if (longestPath == null || longestPath.Count == 0)
            {
                return(null);
            }
            return(longestPath);
        }
Пример #2
0
        static void Main(string[] args)
        {
            string wall       = "";
            string input      = "";
            var    MazeSolver = new MazeSolverUsingStack();
            bool   done       = false;

            Console.WriteLine("It is assumed that the user knows each row has a fixed number of characters and only 1 mouse can be in the maze.");
            Console.WriteLine("Create a maze by inputting a string of walls, paths, exits, and 1 mouse.");
            Console.WriteLine();
            Console.ReadLine();
            Console.Clear();
            while (done != true)
            {
                Console.WriteLine("Legend: e - exit");
                Console.WriteLine("Legend: n - mouse");
                Console.WriteLine("Legend: 0 - path");
                Console.WriteLine("Legend: 1 - wall");
                Console.WriteLine("Enter Row: ");
                input = Console.ReadLine();
                input = "1" + input + "1";
                if (MazeSolver.MazeRows.Count == 0)
                {
                    var mazerowstacklength = input.Length;
                    for (int x = 0; x < mazerowstacklength; x++)
                    {
                        wall += "1";
                    }
                    MazeSolver.MazeRows.Push(wall);
                }
                MazeSolver.MazeRows.Push(input);
                Console.WriteLine("Would you like to enter another row? (Y or N)");
                var answer = Console.ReadLine().ToLowerInvariant();
                if (answer == "n")
                {
                    done = true;
                }
                Console.WriteLine();
            }
            Console.Clear();
            MazeSolver.MazeRows.Push(wall);

            MazeCell[,] mazeArray = new MazeCell[MazeSolver.MazeRows.Count, input.Length];

            for (int y = MazeSolver.MazeRows.Count - 1; y >= 0; y--)
            {
                var currentRow = MazeSolver.MazeRows.Pop();
                for (int x = 0; x < input.Length; x++)
                {
                    mazeArray[y, x] = new MazeCell(x, y, currentRow[x]);
                }
            }
            MazeSolver.DisplayMaze(mazeArray);

            var exitCount = 0;

            foreach (var mazeCell in mazeArray)
            {
                if (mazeCell.Character == 'e')
                {
                    exitCount++;
                }
            }
            //if (exitCount > 1)
            //{
            //    MazeSolver.SolveMazeMultipleExits(mazeArray);
            //}
            //else
            //{
            //    MazeSolver.SolveMazeOneExit(mazeArray);
            //}
            MazeSolver.SolveMazeMultipleExits(mazeArray);


            Console.ReadLine();
        }
Пример #3
0
        public void SolveMazeMultipleExitsOld(MazeCell[,] mazeArray)
        {
            bool NotEntryCell = false;
            //ExitCellCount = 0;
            var foundExitCells = 0;

            InitializeMaze(mazeArray);
            bool ExitMethodFlag = false;

            if (ExitCells.Count > 1)
            {
                Console.WriteLine("Would you like to continue search from last exit found(1), or start from entrance(2)?");
                var answer = Console.ReadLine();
                if (answer == "1")
                {
                    ExitMethodFlag = true;
                }
            }

            CurrentCell = EntryCell;
            var storedMazeArray = new MazeCell[mazeArray.GetLength(0), mazeArray.GetLength(1)];

            for (int y = 0; y < mazeArray.GetLength(0); y++)
            {
                for (int x = 0; x < mazeArray.GetLength(1); x++)
                {
                    storedMazeArray[y, x] = new MazeCell(mazeArray[y, x].X, mazeArray[y, x].Y, mazeArray[y, x].Character);
                }
            }

            while (foundExitCells != ExitCells.Count)
            {
                if (NotEntryCell == false)
                {
                    NotEntryCell = true;
                }
                else
                {
                    if (CurrentCell.Character == 'e')
                    {
                        foundExitCells++;
                        storedMazeArray[CurrentCell.Y, CurrentCell.X].Character = '@';
                        mazeArray = new MazeCell[mazeArray.GetLength(0), mazeArray.GetLength(1)];

                        for (int y = 0; y < mazeArray.GetLength(0); y++)
                        {
                            for (int x = 0; x < mazeArray.GetLength(1); x++)
                            {
                                mazeArray[y, x] = new MazeCell(storedMazeArray[y, x].X, storedMazeArray[y, x].Y, storedMazeArray[y, x].Character);
                            }
                        }
                        Console.WriteLine("Refreshed array");

                        if (ExitMethodFlag == true)
                        {
                            CurrentCell = mazeArray[CurrentCell.Y, CurrentCell.X];
                            MazeStack   = new Stack <MazeCell>();
                        }
                        else
                        {
                            CurrentCell = mazeArray[EntryCell.Y, EntryCell.X];
                            MazeStack   = new Stack <MazeCell>();
                        }
                    }
                    else
                    {
                        CurrentCell.Character = '.';
                    }
                }

                if (mazeArray[CurrentCell.Y + 1, CurrentCell.X].Character == '0' || mazeArray[CurrentCell.Y + 1, CurrentCell.X].Character == 'e')
                {
                    MazeStack.Push(mazeArray[CurrentCell.Y + 1, CurrentCell.X]);
                }
                if (mazeArray[CurrentCell.Y - 1, CurrentCell.X].Character == '0' || mazeArray[CurrentCell.Y - 1, CurrentCell.X].Character == 'e')
                {
                    MazeStack.Push(mazeArray[CurrentCell.Y - 1, CurrentCell.X]);
                }
                if (mazeArray[CurrentCell.Y, CurrentCell.X - 1].Character == '0' || mazeArray[CurrentCell.Y, CurrentCell.X - 1].Character == 'e')
                {
                    MazeStack.Push(mazeArray[CurrentCell.Y, CurrentCell.X - 1]);
                }
                if (mazeArray[CurrentCell.Y, CurrentCell.X + 1].Character == '0' || mazeArray[CurrentCell.Y, CurrentCell.X + 1].Character == 'e')
                {
                    MazeStack.Push(mazeArray[CurrentCell.Y, CurrentCell.X + 1]);
                }

                DisplayMaze(mazeArray);

                if (MazeStack.Count <= 0)
                {
                    if (foundExitCells > 0)
                    {
                        Console.WriteLine("Only " + foundExitCells +
                                          " were/was found. Other exits are unreachable.");
                    }
                    else
                    {
                        Console.WriteLine("Unsolvable maze.");
                    }
                    break;
                }
                else
                {
                    CurrentCell = MazeStack.Pop();
                }
            }
        }