동캄의 코딩도장

프로그래머스 level0 [안전지대] 파이썬 본문

코테/프로그래머스 level0

프로그래머스 level0 [안전지대] 파이썬

동 캄 2023. 3. 5. 11:23

https://school.programmers.co.kr/learn/courses/30/lessons/120866

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

# 프로그래머스 안전지대
r = [1, 1, 1, 0, -1, -1, -1, 0]
c = [-1, 0, 1, 1, 1, 0, -1, -1]


def solution(board):
    answer = 0
    n = len(board)
    for i in range(n):
        for j in range(n):
            if board[i][j] == 1:
                for k in range(8):
                    I = r[k]+i
                    J = c[k]+j
                    if 0 <= I < n and 0 <= J < n and board[I][J] != 1:
                        board[I][J] += 2
    for line in board:
        answer += line.count(0)
    return answer

bfs탐색하듯이 코드를 만들면된다. (단 bfs처럼 계속 탐색은 안함)