コード例 #1
0
        public void SolveMazeMultipleExits(MazeCell[,] mazeArray)
        {
            InitializeMaze(mazeArray);
            var storedMazeArray = new MazeCell[mazeArray.GetLength(0), mazeArray.GetLength(1)];

            CopyMaze(storedMazeArray, mazeArray);

            while (ExitCells.Count > 0)
            {
                ExitCell = ExitCells.Dequeue();
                mazeArray[ExitCell.Y, ExitCell.X].IsVisited = false;
                CurrentCell = EntryCell;
                var mazePath = FindLongestPath(mazeArray);

                if (mazePath == null)
                {
                    Console.WriteLine($"Could not find path to {ExitCell}.");
                    Console.WriteLine();
                    mazeArray[ExitCell.Y, ExitCell.X].Character = 'x';
                    mazeArray[ExitCell.Y, ExitCell.X].IsVisited = true;
                    DisplayMaze(mazeArray);
                    MazeStack.Clear();
                }
                else
                {
                    mazePath[0].IsVisited = true;
                    mazePath.Reverse();
                    mazePath.RemoveAt(0);
                    foreach (var mazeCell in mazePath)
                    {
                        if (mazeCell.Character != ExitCell.Character)
                        {
                            mazeArray[mazeCell.Y, mazeCell.X].Character = '.';
                        }
                        else
                        {
                            mazeArray[mazeCell.Y, mazeCell.X].Character = '@';
                            ExitCell.Character = mazeArray[mazeCell.Y, mazeCell.X].Character;
                        }
                        DisplayMaze(mazeArray);
                    }
                    Console.WriteLine($"Longest path from {EntryCell} to {ExitCell} found.");
                    Console.WriteLine();
                    MazeStack.Clear();
                    storedMazeArray[ExitCell.Y, ExitCell.X] = ExitCell;
                    CopyMaze(mazeArray, storedMazeArray);
                }
            }
        }
コード例 #2
0
 private void InitializeMaze(MazeCell[,] mazeArray)
 {
     foreach (var mazeCell in mazeArray)
     {
         if (mazeCell.Character == 'm')
         {
             EntryCell   = mazeCell;
             CurrentCell = EntryCell;
         }
         if (mazeCell.Character == 'e')
         {
             ExitCells.Enqueue(mazeCell);
         }
         if (mazeCell.Character == '0' || mazeCell.Character == 'm')
         {
             mazeCell.IsVisited = false;
         }
         else
         {
             mazeCell.IsVisited = true;
         }
         MazeStack.Clear();
     }
 }