private static List <MazeCell> FindShortestPath(MazeCell entryCell, MazeCell exitCell, MazeCell currentCell, Queue <MazeCell> mazeQueue, MazeCell[,] mazeArray) { currentCell = entryCell; mazeQueue.Enqueue(currentCell); while (mazeQueue.Count > 0) { currentCell = MazeQueue.Dequeue(); mazeArray[currentCell.Y, currentCell.X].IsVisited = true; if (currentCell == exitCell) { break; } if (mazeArray[currentCell.Y - 1, currentCell.X].IsVisited == false) { mazeArray[currentCell.Y - 1, currentCell.X].Parent = mazeArray[currentCell.Y, currentCell.X]; mazeQueue.Enqueue(mazeArray[currentCell.Y - 1, currentCell.X]); } if (mazeArray[currentCell.Y + 1, currentCell.X].IsVisited == false) { mazeArray[currentCell.Y + 1, currentCell.X].Parent = mazeArray[currentCell.Y, currentCell.X]; mazeQueue.Enqueue(mazeArray[currentCell.Y + 1, currentCell.X]); } if (mazeArray[currentCell.Y, currentCell.X - 1].IsVisited == false) { mazeArray[currentCell.Y, currentCell.X - 1].Parent = mazeArray[currentCell.Y, currentCell.X]; mazeQueue.Enqueue(mazeArray[currentCell.Y, currentCell.X - 1]); } if (mazeArray[currentCell.Y, currentCell.X + 1].IsVisited == false) { mazeArray[currentCell.Y, currentCell.X + 1].Parent = mazeArray[currentCell.Y, currentCell.X]; mazeQueue.Enqueue(mazeArray[currentCell.Y, currentCell.X + 1]); } } var mazePath = new List <MazeCell>(); var start = mazeArray[entryCell.Y, entryCell.X]; var exit = mazeArray[exitCell.Y, exitCell.X]; mazePath.Add(exit); while (exit != start) { if (exit.Parent == null) { return(null); } mazePath.Add(exit.Parent); exit = exit.Parent; } mazePath.Reverse(); mazePath.RemoveAt(0); return(mazePath); }