IT/coding study

[acmicpc] 16918. 봄버맨(python)

seyeonHello 2022. 6. 12. 17:00

유형: 시뮬레이션

1. 초기 폭탄 칸을 0, 빈 칸을 -2로 셋팅해놓는다. 

2. 1초 지날 때마다 모든 칸을 1씩 증가시킨다.

    2-1) 칸의 값이 3이라면, 폭탄을 폭발시킨다. => 자신의 칸과 주위의 칸을 -1로 셋팅해놓는다.

3. 출력할 때, 다시 숫자를 원래 기호로 되돌린다.

순서도

import sys
input=sys.stdin.readline

R,C,N=map(int,input().split())
arr=[]
answer=[]
dirs=[(1,0),(0,1),(-1,0),(0,-1)]

for _ in range(R):
    tmp=list(input().strip())
    sample=[]
    for t in tmp:
        if t=='O': sample.append(0)
        else: sample.append(-2)
    arr.append(sample)

for time in range(0,N):
    for row in range(R):
        for col in range(C):
            arr[row][col] += 1
    for row in range(R):
        for col in range(C):
            if arr[row][col] == 3:
                arr[row][col] = -1
                for i in range(4):
                    nr = row + dirs[i][0]
                    nc = col + dirs[i][1]
                    if 0 <= nr < R and 0 <= nc < C and arr[nr][nc]!=3: 
                    	# 값이 "3"인칸을 모두 바꾸기 위해
                        arr[nr][nc] = -1

for i in range(R):
    sample=[]
    for t in arr[i]:
        if t<0: sample.append('.')
        else: sample.append('O')
    answer.append(sample)

for i in range(len(answer)):
    for j in range(len(answer[0])):
        print(answer[i][j], end='')
    print()

https://www.acmicpc.net/problem/16918

 

16918번: 봄버맨

첫째 줄에 R, C, N (1 ≤ R, C, N ≤ 200)이 주어진다. 둘째 줄부터 R개의 줄에 격자판의 초기 상태가 주어진다. 빈 칸은 '.'로, 폭탄은 'O'로 주어진다.

www.acmicpc.net

'IT > coding study' 카테고리의 다른 글

[acmicpc] 18428. 감시 피하기(python)  (0) 2022.07.01
[acmicpc] 10800. 컬러볼(python)  (0) 2022.06.18
[acmicpc] 17142. 연구소 3(python)  (0) 2022.03.16
[acmicpc] 3020. 개똥벌레 (python)  (0) 2022.03.10
[acmicpc] 3190. 뱀 (python)  (0) 2022.03.07