すべてのチャレンジ
中級 1件解決済み 正答率100.0%

Minesweeper Neighbor Counts

Annotate a rectangular board.

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

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

例

例 1
入力:
board = ["*..", "...", "..*"]

期待される出力:
expected = ["*1.", "121", ".1*"]
入力:
std::vector<std::string> board = {"*..", "...", "..*"};

期待される出力:
std::vector<std::string> expected = {"*1.", "121", ".1*"};
入力:
string[] board = new string[] {"*..", "...", "..*"};

期待される出力:
string[] expected = new string[] {"*1.", "121", ".1*"};

解説

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.

例 2
入力:
board = ["..."]

期待される出力:
expected = ["..."]
入力:
std::vector<std::string> board = {"..."};

期待される出力:
std::vector<std::string> expected = {"..."};
入力:
string[] board = new string[] {"..."};

期待される出力:
string[] expected = new string[] {"..."};

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

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

ヒント 2

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

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 Н"Никита "Programmer" Python 1,140 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が1件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。