Problema
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.
Vincoli
1 ≤ rows, columns ≤ 200. Every row has the same nonzero length and contains only * and . .
Esempi
Input:
board = ["*..", "...", "..*"]
Output atteso:
expected = ["*1.", "121", ".1*"]
Input:
std::vector<std::string> board = {"*..", "...", "..*"};
Output atteso:
std::vector<std::string> expected = {"*1.", "121", ".1*"};
Input:
string[] board = new string[] {"*..", "...", "..*"};
Output atteso:
string[] expected = new string[] {"*1.", "121", ".1*"};
Spiegazione
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.
Input:
board = ["..."]
Output atteso:
expected = ["..."]
Input:
std::vector<std::string> board = {"..."};
Output atteso:
std::vector<std::string> expected = {"..."};
Input:
string[] board = new string[] {"..."};
Output atteso:
string[] expected = new string[] {"..."};
Spiegazione
This one-row board has no mines. All counts are zero, so every dot remains a dot.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Solo le celle sicure cambiano; le mine restano esattamente dove sono.
Suggerimento 2
Per ogni punto, controlla le otto posizioni intorno e conta le mine vicine.
Suggerimento 3
Fai attenzione ai bordi e agli angoli per non leggere fuori dalla griglia.
Complessità attesa
tempo O(r*c), spazio aggiuntivo O(r*c)
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.