๐Ÿค–/๋ฐฑ์ค€

๋ฐฑ์ค€ 16918: ๋ด„๋ฒ„๋งจ (Python)

sssbin 2023. 5. 2. 13:35

 

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

 

16918๋ฒˆ: ๋ด„๋ฒ„๋งจ

์ฒซ์งธ ์ค„์— R, C, N (1 ≤ R, C, N ≤ 200)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ R๊ฐœ์˜ ์ค„์— ๊ฒฉ์žํŒ์˜ ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋นˆ ์นธ์€ '.'๋กœ, ํญํƒ„์€ 'O'๋กœ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

n=0์ผ ๋•Œ: ํญํƒ„ ์„ค์น˜

n=1์ผ ๋•Œ: ๊ทธ๋Œ€๋กœ

n=2์ผ ๋•Œ~: ๋‚˜๋จธ์ง€ ๋ชจ๋“  ์นธ์— ํญํƒ„ ์„ค์น˜ -> ํญ๋ฐœ ๋ฐ˜๋ณต

 

r*c ํฌ๊ธฐ์˜ bombs ๋ฐฐ์—ด์„ ๋ชจ๋‘ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ ,

์ดˆ๊ธฐ ์ƒํƒœ ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ 'O'์ธ ๋ถ€๋ถ„์€ 2๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. (0์ดˆ - 1์ดˆ ์ƒํƒœ)

 

i=2๋ถ€ํ„ฐ n+1๊นŒ์ง€ for๋ฌธ์„ ๋Œ๋ ค์ฃผ๋ฉด์„œ

1. bombs ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๊ฐ์†Œ์‹œํ‚ค๊ณ ,

2. i๊ฐ€ ์ง์ˆ˜์ผ ๋•Œ์—๋Š” ๋ชจ๋“  ์นธ์— ํญํƒ„ ์„ค์น˜ -> 0์ธ ๋ถ€๋ถ„์„ 3์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. (3์ดˆ ํ›„์— ํญํƒ„์„ ํญ๋ฐœ์‹œํ‚ฌ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ)

    i๊ฐ€ ํ™€์ˆ˜์ผ ๋•Œ์—๋Š” ํญํƒ„์„ ํญ๋ฐœ์‹œํ‚จ๋‹ค. -> 0์ธ ๋ถ€๋ถ„์„ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ํญ๋ฐœ์‹œํ‚จ๋‹ค.

 

๋งˆ์ง€๋ง‰์— 0์ธ ๋ถ€๋ถ„์€ '.'์œผ๋กœ ๋ฐ”๊ฟ”์„œ, ์•„๋‹Œ ๋ถ€๋ถ„์€ 'O'๋กœ ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

import copy

def explode(x, y, r, c):
    dx = [1, -1, 0, 0]
    dy = [0, 0, 1, -1]
  
    for i in range(4):
        mx = x + dx[i]
        my = y + dy[i]

        if mx < 0 or mx >= r or my < 0 or my >= c:
            continue

        temp[mx][my] = 0

r, c, n = map(int, input().split())

bombs = [[0] * c for _ in range(r)]
for i in range(r):
    temp = list(input())
    for j in range(c):
        if temp[j] == 'O':
            bombs[i][j] = 2

for i in range(2, n+1):
    for x in range(r): # ๊ฐ’ ํ•˜๋‚˜์”ฉ ๊ฐ์†Œ
        for y in range(c):
            if bombs[x][y] != 0:
                bombs[x][y] -= 1

    if i % 2 == 0: # ๋ชจ๋“  ์นธ์— ํญํƒ„ ์„ค์น˜
        for x in range(r):
            for y in range(c):
                if bombs[x][y] == 0:
                    bombs[x][y] = 3
    else: # 3์ดˆ๊ฐ€ ์ง€๋‚œ ํญํƒ„ ํญ๋ฐœ
        temp = copy.deepcopy(bombs)
        for x in range(r):
            for y in range(c):
                if bombs[x][y] == 0:
                    explode(x, y, r, c)
        bombs = temp[:]

for i in range(r):
    for j in range(c):
        if bombs[i][j] == 0:
            bombs[i][j] = '.'
        else:
            bombs[i][j] = 'O'
        print(bombs[i][j], end='')
    print()