Tất cả thử thách
Trung bình 1 người đã giải Tỷ lệ chấp nhận 100.0%

Minesweeper Neighbor Counts

Annotate a rectangular board.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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

Ví dụ

Ví dụ 1
Đầu vào:
board = ["*..", "...", "..*"]

Kết quả mong đợi:
expected = ["*1.", "121", ".1*"]
Đầu vào:
std::vector<std::string> board = {"*..", "...", "..*"};

Kết quả mong đợi:
std::vector<std::string> expected = {"*1.", "121", ".1*"};
Đầu vào:
string[] board = new string[] {"*..", "...", "..*"};

Kết quả mong đợi:
string[] expected = new string[] {"*1.", "121", ".1*"};

Giải thích

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.

Ví dụ 2
Đầu vào:
board = ["..."]

Kết quả mong đợi:
expected = ["..."]
Đầu vào:
std::vector<std::string> board = {"..."};

Kết quả mong đợi:
std::vector<std::string> expected = {"..."};
Đầu vào:
string[] board = new string[] {"..."};

Kết quả mong đợi:
string[] expected = new string[] {"..."};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 Н"Никита "Programmer" Python 1,140 ms - 5/5 Xem code

Học hỏi từ người khác

Lời giải cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.