DevYoon

[๋ฐฑ์ค€] 21610. ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ๋น„๋ฐ”๋ผ๊ธฐ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 21610. ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ๋น„๋ฐ”๋ผ๊ธฐ (Python)

gimewn 2022. 8. 27. 13:37

link ๐Ÿ”— https://www.acmicpc.net/problem/21610

 

21610๋ฒˆ: ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ๋น„๋ฐ”๋ผ๊ธฐ

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๋Š” ํŒŒ์ด์–ด๋ณผ, ํ† ๋„ค์ด๋„, ํŒŒ์ด์–ด์Šคํ†ฐ, ๋ฌผ๋ณต์‚ฌ๋ฒ„๊ทธ ๋งˆ๋ฒ•์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜ ์ƒˆ๋กœ ๋ฐฐ์šด ๋งˆ๋ฒ•์€ ๋น„๋ฐ”๋ผ๊ธฐ์ด๋‹ค. ๋น„๋ฐ”๋ผ๊ธฐ๋ฅผ ์‹œ์ „ํ•˜๋ฉด ํ•˜๋Š˜์— ๋น„๊ตฌ๋ฆ„์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜์€ ๋น„๋ฐ”๋ผ๊ธฐ

www.acmicpc.net

 

โœ๏ธ ๊ตฌ๋ฆ„์ด ์ด๋™ํ•  ๋• ์ฒซ๋ฒˆ์งธ ํ–‰,์—ด๊ณผ ๋งˆ์ง€๋ง‰ ํ–‰,์—ด์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€๋งŒ ๋Œ€๊ฐ์„ ์— ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ํ™•์ธํ•  ๋• ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์„ ์ž˜ ์ƒ๊ฐํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

โœ๏ธ ์ฒ˜์Œ ํ‘ผ ์ฝ”๋“œ๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ๊ตฌ๋ฆ„์ด ์‚ฌ๋ผ์ง„ ์นธ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์—์„œ not in์„ ์ผ๋Š”๋ฐ, ์ด๋ฅผ ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฒดํฌํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ๋ฐ”๊พธ์–ด ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค. 

 

๐Ÿ“Œ ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚œ ์ฝ”๋“œ

import sys
input = sys.stdin.readline

def movCloud(lst, direction, count):
    for idx in range(len(lst)):
        for num in range(2):
            lst[idx][num] += dir[direction][num]*(count%N)
            if lst[idx][num] >= N:
                lst[idx][num] = lst[idx][num]%N
            elif lst[idx][num] < 0:
                lst[idx][num] = lst[idx][num]+N
    return lst

def checkDgs(y, x):
    isWater = 0
    checkDir = [(-1, -1), (-1, 1), (1, 1), (1, -1)]
    for i, j in checkDir:
        dy, dx = y+i, x+j
        if 0 <= dy < N and 0 <= dx < N:
            if board[dy][dx] > 0:
                isWater += 1
    return isWater

N, M = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
move = [list(map(int, input().split())) for _ in range(M)] # ๋ฐฉํ–ฅ, ๊ฑฐ๋ฆฌ
dir = [(0, 0), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)] # 8๋ฐฉํ–ฅ
cloud = [[N-1, 0], [N-1, 1], [N-2, 0], [N-2, 1]]

for idx in range(M):
    clound = movCloud(cloud, move[idx][0], move[idx][1])
    for y, x in clound:
        board[y][x] += 1
    for y, x in cloud:
        board[y][x] += checkDgs(y, x)
    newCloud = []
    for y in range(N):
        for x in range(N):
            if board[y][x] >= 2 and [y, x] not in cloud:
                newCloud.append([y, x])
                board[y][x] -= 2
    cloud = newCloud

res = 0

for idx in range(N):
    res += sum(board[idx])

print(res)

 

 

๐Ÿ“Œ ์ •๋‹ต ์ฝ”๋“œ

import sys
input = sys.stdin.readline

def movCloud(lst, direction, count):
    for idx in range(len(lst)):
        for num in range(2):
            lst[idx][num] += dir[direction][num]*(count%N) # if count๊ฐ€ 50์ด๊ณ  N์ด 5์ด๋ฉด ์›€์ง์ผ ํ•„์š” X
            if lst[idx][num] >= N: # ์ฒ˜์Œ๊ณผ ๋์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ N์ด์ƒ์ผ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
                lst[idx][num] = lst[idx][num]%N
            elif lst[idx][num] < 0: # ๋™์ผํ•œ ์ด์œ ๋กœ ์Œ์ˆ˜์ผ ๋•Œ ์ฒ˜๋ฆฌ
                lst[idx][num] = lst[idx][num]+N
    return lst

def checkDgs(y, x): # ๋Œ€๊ฐ์„  ์ค‘ ๋ฐ”๊ตฌ๋‹ˆ์— ๋ฌผ ์žˆ๋Š” ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    isWater = 0
    checkDir = [(-1, -1), (-1, 1), (1, 1), (1, -1)]
    for i, j in checkDir:
        dy, dx = y+i, x+j
        if 0 <= dy < N and 0 <= dx < N:
            if board[dy][dx] > 0:
                isWater += 1
    return isWater

N, M = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
move = [list(map(int, input().split())) for _ in range(M)] # ๋ฐฉํ–ฅ, ๊ฑฐ๋ฆฌ
dir = [(0, 0), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)] # 8๋ฐฉํ–ฅ
cloud = [[N-1, 0], [N-1, 1], [N-2, 0], [N-2, 1]] # ์ดˆ๊ธฐ ๊ตฌ๋ฆ„ ์œ„์น˜

for idx in range(M):
    clound = movCloud(cloud, move[idx][0], move[idx][1]) # ๊ตฌ๋ฆ„ ์›€์ง์—ฌ์ฃผ๊ธฐ
    checkCloud = [[0]*N for _ in range(N)] # ๊ตฌ๋ฆ„ ์žˆ๋Š” ๊ณณ ์ฒดํฌ ์šฉ๋„
    for y, x in clound:
        board[y][x] += 1 # ๊ตฌ๋ฆ„ ์žˆ๋Š” ์นธ ๋ฐ”๊ตฌ๋‹ˆ์— ๋ฌผ +1
        checkCloud[y][x] = 1 # ๊ตฌ๋ฆ„ ์žˆ๋‹ค๊ณ  ์ฒดํฌ
    for y, x in cloud:
        board[y][x] += checkDgs(y, x) # ๋Œ€๊ฐ์„ ์— ๋ฌผ ์žˆ๋Š” ์ˆ˜๋งŒํผ ๋ฌผ ์ฆ๊ฐ€
    newCloud = []
    for y in range(N):
        for x in range(N):
            if board[y][x] >= 2 and checkCloud[y][x] == 0: # ๋ฐ”๊ตฌ๋‹ˆ์— ๋ฌผ์ด 2 ์ด์ƒ ์žˆ๊ณ , ๊ตฌ๋ฆ„ ์žˆ๋˜ ์นธ์ด ์•„๋‹ˆ๋ฉด
                newCloud.append([y, x]) # ์ƒˆ๋กœ์šด ๊ตฌ๋ฆ„ ์นธ์œผ๋กœ ์ถ”๊ฐ€
                board[y][x] -= 2 # ๋ฌผ 2 ๊ฐ์†Œ
    cloud = newCloud # ๊ตฌ๋ฆ„ ๋ฆฌ์ŠคํŠธ ๊ต์ฒด

res = 0

for idx in range(N):
    res += sum(board[idx]) # ๊ฐ ํ–‰๋งˆ๋‹ค ๋ฌผ์˜ ์–‘ ๋”ํ•ด์ฃผ๊ธฐ

print(res)