문제풀이/구현

● [파이썬] [구현] 백준 21610 마법사 상어와 비바라기

승무_ 2023. 3. 22. 12:43

문제

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

 

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

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

www.acmicpc.net

코드

import sys
input=sys.stdin.readline

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

n,m=map(int, input().split())
array=[list(map(int, input().split())) for _ in range(n)]
cloud = [[False] * n for _ in range(n)]

for i in range(m):
    d,s=map(int, input().split())

    if i == 0:
        cloud[n-1][0]=True
        cloud[n-1][1]=True
        cloud[n-2][0]=True
        cloud[n-2][1]=True

    # temp에 한번에 보았다가 구름 이동
    temp=[]
    for j in range(n):
        for k in range(n):
            tempj=j
            tempk=k
            if cloud[j][k]==True:
                nj=j+dx[d-1]*s
                nk=k+dy[d-1]*s
                nj=nj%n
                nk=nk%n
                cloud[tempj][tempk]=False
                temp.append([nj,nk])

    for j,k in temp:
        cloud[j][k]=True
        array[j][k]+=1

    for j in range(n):
        for k in range(n):
            if cloud[j][k]==True:
                count=0
                # 대각선에 1이상이 있는 경우
                for l in 1,3,5,7:
                    nx=j+dx[l]
                    ny=k+dy[l]
                    if 0<=nx<n and 0<=ny<n:
                        if array[nx][ny]>0:
                            count+=1
                array[j][k]+=count

    for j in range(n):
        for k in range(n):
            # 구름이 생기는 칸은 3에서 구름이 사라진 칸이 아니어야 한다.
            if cloud[j][k]==True:
                cloud[j][k]=False
            else:
                if array[j][k]>=2:
                    cloud[j][k]=True
                    array[j][k]-=2
result=0
for i in range(n):
    for j in range(n):
        result+=array[i][j]

print(result)

생각 정리

-1%5 = 4

-2%5 = 3

-3%5 = 2

-4%5 = 1

-5%5 = 0

-6%5 = 4