-
[백준] 16918번 봄버맨(python)Python/BAEKJOON 2022. 1. 4. 23:39728x90
문제)
알고리즘)
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'Python > BAEKJOON' 카테고리의 다른 글
[백준] 1260번 DFS와 BFS(python) (0) 2022.01.29 [백준] 2606번 바이러스(python) (0) 2022.01.23 [백준] 8911번 거북이(python) (0) 2022.01.21 [백준] 3048번 개미(python) (0) 2022.01.19 [백준] 2841번 외계인의 기타연주(python) (0) 2022.01.08