[백준] 16918 - 봄버맨 / Python / 실버 1

KimYoungWoong·2022년 9월 15일
0

BOJ

목록 보기
13/31
post-thumbnail

🚩문제 주소


📄풀이


폭탄의 위치를 찾는 함수, '.'을 폭탄으로 바꿔주는 함수, 폭탄을 터뜨려 '.'로 다시 바꾸는 함수 총 3개를 만듭니다.

처음에 입력하는 것을 1단계라고 생각하고, n을 1 감소시켜서 2단계를 지나가줍니다.
폭탄을 생성하는 함수와 폭탄을 터뜨리는 함수를 n이 0이 될 때까지 반복시켜줍니다.

join 함수를 이용해 각 배열을 공백없이 차례로 출력해줍니다.



👨‍💻코드


from collections import deque

def checkBomb():
  for i in range(r):
    for j in range(c):
      if board[i][j] == 'O':
        bombs.append([i,j])

def makeBomb():
  for i in range(r):
    for j in range(c):
      if board[i][j] == '.':
        board[i][j] = 'O'

def explosion():
  dx, dy = [1,0,-1,0], [0,-1,0,1]
  while bombs:
    x, y = bombs.popleft()
    board[x][y] = '.'
    for i in range(4):
      nx = x + dx[i]
      ny = y + dy[i]
      if 0<=nx<r and 0<=ny<c:
        board[nx][ny] = '.'

r, c, n = map(int, input().split())
board = [list(input()) for _ in range(r)] # 1단계

n -= 1 # 2단계
while n:
  bombs = deque()
  checkBomb()
  makeBomb() # 3단계
  n-=1
  if n == 0:
    break
  explosion() # 4단계
  n-=1

for i in range(r):
  print(''.join(board[i]))

profile
블로그 이전했습니다!! https://highero.tistory.com

0개의 댓글