아주 오랜만에 풀어서 감을 잃어, 타 블로그도 참고해보았다. (구름 칸 인덱스를 담는 배열 생성하기!)
[단계별 풀이]
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
'IT > coding study' 카테고리의 다른 글
[acmicpc] 2252. 줄 세우기(python) (0) | 2021.12.01 |
---|---|
[acmicpc] 19238. 스타트 택시(python) (0) | 2021.10.24 |
[programmers] 베스트앨범(python) (0) | 2021.03.18 |
[acmicpc] 11656. 접미사 배열(python) (0) | 2021.01.28 |
[acmicpc] 7576. 토마토(BFS) (0) | 2020.09.26 |