Python/BAEKJOON

[백준] 16918번 봄버맨(python)

Magin 2022. 1. 4. 23:39
728x90

문제)

 

 

알고리즘)

1) 처음 폭탄 위치를 확인(boom에 저장)

--------------------------반복-----------------------

(조건: n 값이 0이 아닌동안)

2) 모든 구역을 'O'변환

3) boom에 저장된 폭탄 위치에서 폭탄 폭발

------------------------------------------------------

fin) 최종 출력

 

 

 

 

코드)

#봄버맨
import sys
from collections import deque

move = [[-1,0],[1, 0],[0, -1],[0, 1]] #폭탄 폭발 범위

def col_boom(): # 처음 폭탄위치
    for index_1 in range(r):
        for index_2 in range(c):
            if graphy[index_1][index_2] == 'O':
                boom.append([index_1, index_2])


def all_boom(): #모든 위치 폭탄
    for index_1 in range(r):
            for index_2 in range(c):
                if graphy[index_1][index_2] == '.':
                    graphy[index_1][index_2] = 'O'


def boom_boom(): #폭탄 폭발
    while boom:
        boom_x, boom_y = boom.popleft()
        graphy[boom_x][boom_y] = '.'
       
        #폭탄 폭탄 구현
        for move_x, move_y in move:
            test_x, test_y = boom_x+move_x, boom_y+move_y
            if 0 <= test_x < r:
                result_x = test_x
            else:
                result_x = boom_x

            if 0 <= test_y < c:
                result_y = test_y
            else:
                result_y = boom_y
            if graphy[result_x][result_y] == 'O':
                graphy[result_x][result_y] = '.'

r, c, n = map(int, sys.stdin.readline().split())
graphy = [list(sys.stdin.readline().rstrip()) for _ in range(r)]
boom = deque()

n -= 1

#반복과정
while n:
    col_boom()
    if n == 0:
        break
   
    n -= 1
    all_boom()
    if n == 0:
        break

    n -= 1
    boom_boom()
   
#최종 출력
for a in graphy:
    for b in a:
        print(b, end='')
    print()
728x90