IT/coding study

[acmicpc] 21610. 마법사 상어와 비바라기(python)

seyeonHello 2021. 10. 20. 00:12

아주 오랜만에 풀어서 감을 잃어, 타 블로그도 참고해보았다. (구름 칸 인덱스를 담는 배열 생성하기!)

 

[단계별 풀이]

1. 구름칸 이동하기

2. 구름칸에 1씩 더하기

3. 구름칸 비우기

4. 구름칸에 각 대각선 물의 개수 더하기

5. 구름 만들기 (물의 양 2이상, 이전 구름칸 미포함)

 


정답코드

import sys
input=sys.stdin.readline

diry=[0,-1,-1,-1,0,1,1,1]
dirx=[-1,-1,0,1,1,1,0,-1]
# 대각선: 1, 3, 5, 7

if __name__ == "__main__":
    N,M=map(int,input().split())
    maps=[]
    methods=[]
    clouds=[[N-2,0],[N-2,1],[N-1,0],[N-1,1]]
    for _ in range(N):
        maps.append(list(map(int,input().split())))
    for _ in range(M):
        d,s=map(int,input().split())
        next_clouds=[]

        # 1단계.
        for cloud in clouds:
            nx=(cloud[1]+dirx[d-1]*s)%N
            ny=(cloud[0]+diry[d-1]*s)%N
            next_clouds.append([ny,nx])

        # 2단계.
        visited=[[0]*N for _ in range(N)]
        for cloud in next_clouds:
            maps[cloud[0]][cloud[1]]+=1
            visited[cloud[0]][cloud[1]]=1

        # 3단계.
        clouds=[]

        # 4단계.
        for cloud in next_clouds:
            cnt=0
            for i in range(1,8,2):
                ny=cloud[0]+diry[i]
                nx=cloud[1]+dirx[i]

                if 0<=ny<N and 0<=nx<N and maps[ny][nx]>=1:
                    cnt+=1

            maps[cloud[0]][cloud[1]]+=cnt

        # 5단계.
        for y in range(N):
            for x in range(N):
                if maps[y][x]>=2 and visited[y][x]==0:
                    maps[y][x]-=2
                    clouds.append([y,x])

    answer=0
    for map in maps:
        answer+=sum(map)
    print(answer)

 

 


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

 

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

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

www.acmicpc.net