# 2차원 배열 행row, 열col이 입력되고, 그 다음 줄부터 배열의 요소로 들어갈 문자가 입력된다.
# * = 지뢰
# . 은 지뢰가 아니다.
# 지뢰가 아닌 요소에 인접한 지뢰의 수를 출력하는 프로그램을 작성해보자.
"""
입력
3 3
.**
*..
.*.
"""
row, col = map(int, input().split())
matrix = []
for i in range(row):
matrix.append(list(input()))
bomb_result= [] # 지뢰 찾기 결과용 배열 생성
bomb_result=matrix[:] # 입력이 끝난 배열 matrix의 값을 배열 bomb_result에 복사
check = [[-1,-1], [-1,0], [-1, 1],
[0,-1], [0, 1],
[1,-1], [1,0], [1, 1]] #체크 위치로부터의 상대적 좌표
for i in range(row):
for j in range(col):
if matrix[i][j] == '*':
continue
cnt = 0
for dx, dy in check:
nx, ny = i+dx, j+dy
if 0 <= nx <row and 0 <= ny < col and matrix[nx][ny] == '*':
cnt += 1
bomb_result[i][j] = str(cnt)
for i in range(row):
print(''.join(bomb_result[i]))
To embed this project on your website, copy the following code and paste it into your website's HTML: