모든 챌린지
보통 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

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 ННикита Python 1,140 ms - 5/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 1개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.