Exemplo n.º 1
0
 static void AddNewCellToQ(Cell cell, int deltaRow, int deltaCol)
 {
     if (CellIsInAvailable(cell, deltaRow, deltaCol))
     {
         cellsToVisit.Add(new Cell(cell.Row + deltaRow, cell.Col + deltaCol, stepsCounter));
     }
 }
Exemplo n.º 2
0
        static bool CellIsInAvailable(Cell cell, int deltaRow, int deltaCol)
        {
            int newRow = cell.Row + deltaRow;
            int newCol = cell.Col + deltaCol;
            bool rowIsValid = newRow >= 0 && newRow < rows;
            bool colIsValid = newCol >= 0 && newCol < cols;
            bool visited = true;
            if (rowIsValid && colIsValid)
            {
                visited = board[newRow, newCol] != 0;
            }

            return !visited;
        }
Exemplo n.º 3
0
 static void BFS(int row, int col)
 {
     Queue<Cell> cellsQ = new Queue<Cell>();
     Cell startCell = new Cell(row, col, stepsCounter++);
     cellsQ.Enqueue(startCell);
     while(cellsQ.Count > 0)
     {
         var currentCell = cellsQ.Dequeue();
         SetBoardCellValue(currentCell);
         var availablePositions = GetPossibleNextMoves(currentCell);
         foreach (Cell cell in availablePositions)
         {
             cell.Value = currentCell.Value + 1;
             cellsQ.Enqueue(cell);
         }
     }
 }
Exemplo n.º 4
0
 static IList<Cell> GetPossibleNextMoves(Cell cell)
 {
     cellsToVisit.Clear();
     AddNewCellToQ(cell, -1, -2);
     AddNewCellToQ(cell, -2, -1);
     AddNewCellToQ(cell, -2, +1);
     AddNewCellToQ(cell, -1, +2);
     AddNewCellToQ(cell, +1, +2);
     AddNewCellToQ(cell, +2, +1);
     AddNewCellToQ(cell, +2, -1);
     AddNewCellToQ(cell, +1, -2);
     return cellsToVisit;
 }
Exemplo n.º 5
0
 static void SetBoardCellValue(Cell cell)
 {
     board[cell.Row, cell.Col] = cell.Value;
 }