Ejemplo n.º 1
0
        static List<Cell> GetPossibleMoves(string[,] maze, Cell currentCell, string freeSymbol)
        {
            var result = new List<Cell>();
            var totalRows = maze.GetLength(0);
            var totalCols = maze.GetLength(1);
            var currentRow = currentCell.Row;
            var currentCol = currentCell.Col;

            // check cell to the left
            if(currentCol > 0 && maze[currentRow, currentCol - 1] == freeSymbol)
            {
                result.Add(new Cell(currentRow, currentCol - 1, currentCell.Value));
            }
            // check cell above
            if (currentRow > 0 && maze[currentRow - 1, currentCol] == freeSymbol)
            {
                result.Add(new Cell(currentRow - 1, currentCol, currentCell.Value));
            }
            // check cell to the right
            if (currentCol < totalCols - 1 && maze[currentRow, currentCol + 1] == freeSymbol)
            {
                result.Add(new Cell(currentRow, currentCol + 1, currentCell.Value));
            }
            // check cell below
            if (currentRow < totalRows - 1 && maze[currentRow + 1, currentCol] == freeSymbol)
            {
                result.Add(new Cell(currentRow + 1, currentCol, currentCell.Value));
            }

            return result;
        }
Ejemplo n.º 2
0
 static void BFS(string[,] maze, Cell start, string freeString)
 {
     var queue = new Queue<Cell>();
     var moves = new List<Cell>();
     queue.Enqueue(start);
     while (queue.Count > 0)
     {
         var currentElement = queue.Dequeue();
         moves = GetPossibleMoves(maze, currentElement, freeString);
         foreach (var move in moves)
         {
             move.Value++;
             queue.Enqueue(move);
             maze[move.Row, move.Col] = move.Value.ToString();
         }
     }
 }