private static void CalculateDistanceBFS(string[,] labyrinth, Cell startCell) { Queue<Cell> cells = new Queue<Cell>(); cells.Enqueue(startCell); while (cells.Count > 0) { var currentCell = cells.Dequeue(); var currentRow = currentCell.Row; var currentCol = currentCell.Col; var currentValue = currentCell.Value + 1; if (currentRow + 1 < labyrinth.GetLength(0) && labyrinth[currentRow + 1, currentCol] == "0") { labyrinth[currentRow + 1, currentCol] = currentValue.ToString(); cells.Enqueue(new Cell(currentRow + 1, currentCol, currentValue)); } if (currentRow - 1 >= 0 && labyrinth[currentRow - 1, currentCol] == "0") { labyrinth[currentRow - 1, currentCol] = currentValue.ToString(); cells.Enqueue(new Cell(currentRow - 1, currentCol, currentValue)); } if (currentCol + 1 < labyrinth.GetLength(1) && labyrinth[currentRow, currentCol + 1] == "0") { labyrinth[currentRow, currentCol + 1] = currentValue.ToString(); cells.Enqueue(new Cell(currentRow, currentCol + 1, currentValue)); } if (currentCol - 1 >= 0 && labyrinth[currentRow, currentCol - 1] == "0") { labyrinth[currentRow, currentCol - 1] = currentValue.ToString(); cells.Enqueue(new Cell(currentRow, currentCol - 1, currentValue)); } } }
private static Cell GetStartCell(string[,] labyrinth) { for (int row = 0; row < labyrinth.GetLength(0); row++) { for (int col = 0; col < labyrinth.GetLength(1); col++) { var currentValue = labyrinth[row, col]; if (currentValue == "*") { var startCell = new Cell(row, col, 0); return startCell; } } } throw new ArgumentException("There is no start cell!"); }
private static void GetDistanceInLabyrinth(string[,] labyrinth, Cell startCell) { CalculateDistanceBFS(labyrinth, startCell); FillUnreachableCells(labyrinth); }