문제
https://www.acmicpc.net/problem/21610
코드
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
'문제풀이 > 구현' 카테고리의 다른 글
○ [파이썬] [구현] 백준 15683 감시 (0) | 2023.03.27 |
---|---|
○ [파이썬] [구현] 백준 14499 주사위 굴리기 (0) | 2023.03.25 |
@ [파이썬] [구현] 백준 21608 상어 초등학교 (0) | 2023.03.20 |
● [파이썬] [구현] 백준 14891 톱니바퀴 (0) | 2023.03.18 |
[파이썬] [구현] 백준 14889 스타트와 링크 (0) | 2023.03.17 |