Simple iterative solution to counts mines in Minesweeper
- Provide a 2D list of a rectangular board with
-1
representing a mine and0
in all other spaces - call
displayMines(board)
passing in the board and the number of mines adjacent to each square will be printed preserving mines as-1
- Works for non-square dimensions
- Copies the board to a new board where the counts will reside
- Loops over all squares in the input board
- If a mine is encountered, it checks the 8 surrounding squares & increments the count in each square that isn't a mine
- Surrounding squares are checked by a nested for loop,
xTemp
coordinates go fromx-1
tox+1
&yTemp
coordinates go fromy-1
toy+1
- All coordinates are checked to make sure they don't go out of bounds of the edges of the board
- The current square is skipped by also checking if the value at
board[ytemp]pxTemp]
is a mine before incrementing
- Surrounding squares are checked by a nested for loop,
O(mn) for looping over the entire board
O(8) for checking surrounding squares
O(8*mn) combined
⇒ O(mn)