Retour aux défis
Moyen 0 résolus 0 % de réussite

Minesweeper Neighbor Counts

Annotate a rectangular board.

Ce que vous allez apprendre

  • Visit the 8 neighbors of a cell
  • Stay inside the grid boundaries
  • Build an annotated copy of a board

Problème

Afficher la traduction

Annotate a rectangular board. A star (*) is a mine and a dot (.) is a safe cell. For each safe cell, count mines in the eight surrounding cells, including diagonals. Replace it with the count from 1 to 8, or keep a dot if the count is zero. Preserve every mine. Cells outside the board do not exist.

Implement minesweeper_neighbor_counts(board).

Input

board: The rows from top to bottom; characters run left to right.

Output

Return the annotated rows in the same order and dimensions.

Contraintes

1 ≤ rows, columns ≤ 200. Every row has the same nonzero length and contains only * and . .

Exemples

Exemple 1
Entrée :
board = ["*..", "...", "..*"]

Sortie attendue :
expected = ["*1.", "121", ".1*"]
Entrée :
std::vector<std::string> board = {"*..", "...", "..*"};

Sortie attendue :
std::vector<std::string> expected = {"*1.", "121", ".1*"};
Entrée :
string[] board = new string[] {"*..", "...", "..*"};

Sortie attendue :
string[] expected = new string[] {"*1.", "121", ".1*"};

Explication

The center touches both mines, so its count is 2. The top-right and bottom-left touch none and stay dots. Other safe cells touch one mine.

Exemple 2
Entrée :
board = ["..."]

Sortie attendue :
expected = ["..."]
Entrée :
std::vector<std::string> board = {"..."};

Sortie attendue :
std::vector<std::string> expected = {"..."};
Entrée :
string[] board = new string[] {"..."};

Sortie attendue :
string[] expected = new string[] {"..."};

Explication

This one-row board has no mines. All counts are zero, so every dot remains a dot.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Only safe cells change; mines stay exactly where they are.

Indice 2

For each dot, inspect the eight surrounding positions and count nearby mines.

Indice 3

Be careful near edges and corners so you do not read outside the board.

Complexité attendue

O(r*c) time, O(r*c) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.