챌린지 목록으로 돌아가기
보통 0명 해결 정답률 0%

Minesweeper Neighbor Counts

Annotate a rectangular board.

배우게 될 내용

  • 칸의 8방향 이웃 살펴보기
  • 격자 범위 안에 머물기
  • 주석이 달린 판 복사본 만들기

문제

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

안전한 칸만 바뀌고, 지뢰는 원래 위치 그대로 둡니다.

힌트 2

각 dot에 대해 주변 8칸을 확인해 인접한 지뢰 수를 세세요.

힌트 3

가장자리와 모서리에서는 보드 밖을 읽지 않도록 주의하세요.

예상 복잡도

시간 O(r*c), 추가 공간 O(r*c)

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.