알고리즘
[프로그래머스, 파이썬] 안전지대
hminor
2024. 2. 21. 10:01
반응형
풀이
- 지뢰가 있는 곳을 찾은 다음
- 해당 위치에서 board 칸을 벗어나지 않는지 확인 후
- 해당 칸이 0으로 비어있다면 2로 만들어 위험지역으로 표시한 뒤
- 다시 반복문을 활용하여 해결
def solution(board):
result = 0
hei,wid = len(board),len(board[0])
for i in range(hei):
for j in range(wid):
if board[i][j] == 1:
for a,b in [[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1]]:
if 0<=i+a<hei and 0<=j+b<wid and board[i+a][j+b] == 0: board[i+a][j+b] = 2
for i in board: result += i.count(0)
return result