알고리즘

[백준] 21610. 마법사 상어와 비바라기

담쏙 2021. 10. 11. 20:26
728x90

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

 

21610번: 마법사 상어와 비바라기

마법사 상어는 파이어볼, 토네이도, 파이어스톰, 물복사버그 마법을 할 수 있다. 오늘 새로 배운 마법은 비바라기이다. 비바라기를 시전하면 하늘에 비구름을 만들 수 있다. 오늘은 비바라기

www.acmicpc.net

2021 상반기 삼성 SW역량테스트 오후 기출문제이다.

크게 어려운 건 없는 문제다. 각 기능을 모두 함수로 구현했다. move_clouds는 구름이동, fall_rain은 비 뿌리기, copy_water은 물복사버그 마법, make_clouds는 구름 새로 생성, solve는 총 비의 양 계산이다.

move_clouds 에서 새로운 list를 return 해주었는데, 직접 원소에 접근해서 수정하는 방식이 훨씬 나을 것 같다. 또한 move_clouds와 fall_rain을 한번에 구현해도 될 거 같다.

dy = [0, -1, -1, -1, 0, 1, 1, 1]
dx = [-1, -1, 0, 1, 1, 1, 0, -1]

n, m = map(int, input().split())
maps = [list(map(int, input().split())) for _ in range(n)]
clouds = [(n-1, 0), (n-1, 1), (n-2, 0), (n-2, 1)]


def move_clouds(fd, fs, old_clouds):
    new_clouds = []
    for cloud in old_clouds:
        new_clouds.append(((cloud[0] + dy[fd-1] * fs) % n, (cloud[1] + dx[fd-1] * fs) % n))
    return new_clouds


def fall_rain(rain_cloud):
    for y, x in rain_cloud:
        maps[y][x] += 1


def copy_water(water_clouds):
    for y, x in water_clouds:
        add_water = 0
        for ky, kx in [(-1,-1), (1, 1), (1, -1), (-1, 1)]:
            ny = ky + y
            nx = kx + x
            if 0 <= ny < n and 0 <= nx < n and maps[ny][nx]:
                add_water += 1
        maps[y][x] += add_water


def make_clouds(old_clouds):
    new_clouds = []
    for i in range(n):
        for j in range(n):
            if maps[i][j] >= 2 and (i, j) not in old_clouds:
                maps[i][j] -= 2
                new_clouds.append((i, j))
    return new_clouds


def solve():
    cnt = 0
    for i in range(n):
        for j in range(n):
            if maps[i][j]:
                cnt += maps[i][j]
    return cnt


for _ in range(m):
    d, s = map(int, input().split())
    clouds = move_clouds(d, s, clouds)
    fall_rain(clouds)
    copy_water(clouds)
    clouds = make_clouds(clouds)


print(solve())

'알고리즘' 카테고리의 다른 글

[SWEA] 5656. 벽돌 깨기  (0) 2021.10.12
[SWEA] 2105. 디저트 카페  (0) 2021.10.12
[백준] 1213. 팰린드롬 만들기  (0) 2021.10.10
[백준] 16235. 나무 재테크  (0) 2021.10.07
[백준] 21609. 상어 중학교  (0) 2021.10.05