Skip to content

Instantly share code, notes, and snippets.

@inspector-ambitious
Last active November 12, 2021 06:05
Show Gist options
  • Select an option

  • Save inspector-ambitious/22a57e68d9db56c151bb7afdafe8ead0 to your computer and use it in GitHub Desktop.

Select an option

Save inspector-ambitious/22a57e68d9db56c151bb7afdafe8ead0 to your computer and use it in GitHub Desktop.

Minesweeper

Given a 2D array of mines, replace the question mark with the number of mines that immediately surround it. This includes the diagonals, meaning it is possible for it to be surrounded by 8 mines maximum.

The key is as follows:

  • An empty space: "-"
  • A mine: "#"
  • Number showing number of mines surrounding it: "?"

Examples

minesweeper([
  ["-", "#", "-"],
  ["-", "?", "-"],
  ["-", "-", "-"]
]) ➞ [
  ["-", "#", "-"],
  ["-", "1", "-"],
  ["-", "-", "-"]
]

minesweeper([
  ["-", "#", "-"],
  ["#", "-", "?"],
  ["#", "#", "-"]
]) ➞ [
  ["-", "#", "-"],
  ["#", "-", "2"],
  ["#", "#", "-"]
]

minesweeper([
  ["-", "#", "#"],
  ["?", "#", ""],
  ["#", "?", "-"]
]) ➞ [
  ["-", "#", "#"],
  ["3", "#", ""],
  ["#", "2", "-"]
]

minesweeper([
  ["-", "-", "#"],
  ["?", "-", "-"],
  ["-", "-", "?"]
]) ➞ [
  ["-", "-", "#"],
  ["0", "-", "-"],
  ["-", "-", "0"]
]

Important Considerations

  • You will only be given square grids 3x3, 4x4 etc...
  • The question mark is not limited to the centre.
  • There may be multiple question marks.

Code to complete

std::vector<std::vector<char>> minesweeper(std::vector<std::vector<char>> grid){

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment