yuns

[21610] python 마법사 상어와 비바라기 본문

algorithms/백준

[21610] python 마법사 상어와 비바라기

yuuuun 2022. 3. 1. 22:39
반응형

문제 링크

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

문제 요약

  • 구름이 이동함에 따라 지역마다 물의 양이 감소 혹은 증가함

아이디어 정리

  • 아래 과정이 필요
    1. 이동한 구름의 위치 파악 한 뒤, 물의 양을 증가시킨다.
      • 여기서 배열의 범위를 넘어갈 경우 반대쪽으로 넘어서 돌아오는 것을 고려해야함
    2. 물이 증가한 위치에서 대각선 방향에 있는 물이 있는 지역을 기준으로 물의 양을 증가시킴
      • 1번 과정과 달리 범위를 넘어갈 경우 없다고 판단
    3. 새로운 구름의 위치를 알아내는 과정
      • 전체 지역에서 구름의 위치가 기존 구름과 겹치지 않도록 하면서 2이상인 지역으로 추가

문제 풀이

n, m = map(int, input().split())
maps = []
for _ in range(n):
    maps.append(list(map(int, input().split())))

cloud = [[n - 1, 0], [n - 1, 1], [n - 2, 0], [n - 2, 1]]
def get_cloud(cloud):
    global maps
    new_cloud = []
    for i in range(n):
        for j in range(n):
            if maps[i][j] > 1 and not visited[i][j]:
                new_cloud.append([i, j])
                maps[i][j] -= 2
    return new_cloud

direction = [[], [0, -1], [-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1]]
cloud_dir = [[-1, -1], [-1, 1], [1, -1], [1, 1]]
for _ in range(m):
    dire, cnt = map(int, input().split())
    visited = [[False] * n for _ in range(n)]
    for i in range(len(cloud)):
        x, y = cloud[i][0] + cnt * direction[dire][0], cloud[i][1] + cnt * direction[dire][1]
        
        while x >= n:
            x -= n
        while x < 0:
            x += n
        while y >= n:
            y -= n
        while y < 0:
            y += n
            
        visited[x][y] = True
        maps[x][y] += 1
        cloud[i] = [x, y]
    
    cnt_add_cloud = []
    for x, y in cloud:
        cnt = 0
        for dx, dy in cloud_dir:
            nx, ny = x + dx, y + dy
            if nx >= n or ny >= n or nx < 0 or ny < 0:
                continue
            if maps[nx][ny] > 0:
                cnt += 1
        cnt_add_cloud.append(cnt)
    
    for clo, cnt in zip(cloud, cnt_add_cloud):
        maps[clo[0]][clo[1]] += cnt
    
    cloud = get_cloud(cloud)

ans = 0
for ma in maps:
    ans += sum(ma)
print(ans)
반응형

'algorithms > 백준' 카테고리의 다른 글

[19238] python 스타트 택시  (0) 2022.03.01
[23288] python 주사위 굴리기2  (0) 2022.03.01
[23289] python 주사위 굴리기  (0) 2022.03.01
[21609] python 상어 중학교  (0) 2022.03.01
[23289] python 온풍기 안녕!  (0) 2022.03.01
Comments