알고리즘 공부

[삼성SW역량테스트] 마법사 상어와 비바라기 (백준 21610 )| Python3

유나쒸 2021. 10. 14. 15:55

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
input=sys.stdin.readline
 
def move_cloud(d, s):
    new=[]
    direction=[None, (0,-1),(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1)]
    for x,y in clouds:
        mx,my=direction[d][0]*s, direction[d][1]*s
        new.append([(x+mx)%n, (y+my)%n])
    return new
 
def increase_water_in_cloud(clouds):
    for x,y in clouds: maps[x][y]+=1
 
def waterbug_magic(clouds):
    diagonal=[(-1,-1),(-1,1),(1,-1),(1,1)]
    for x,y in clouds:
        count=0
        for dx, dy in diagonal:
             if 0<=x+dx<and 0<=y+dy<n:
                 if maps[x+dx][y+dy]>0: count+=1
        maps[x][y]+=count
    return
 
def make_cloud(clouds):
    new=[]
    is_in_clouds=[[False]*for _ in range(n)]
    for x,y in clouds: is_in_clouds[x][y]=True
 
    for i in range(n):
        for j in range(n):
            if not is_in_clouds[i][j] and maps[i][j]>=2
                new.append((i,j))
                maps[i][j]-=2
    return new
 
if __name__ =="__main__":
    n,m=map(int, input().split())
    maps=[list(map(int, input().split())) for _ in range(n)]
    instruction=[list(map(int, input().split())) for _ in range(m)] # [[di,si]] 방향과 거리
 
    clouds=[(n-1,0),(n-1,1),(n-2,0),(n-2,1)]
    for i in range(m):
        clouds=move_cloud(instruction[i][0], instruction[i][1])
        increase_water_in_cloud(clouds)
        waterbug_magic(clouds)
        clouds=make_cloud(clouds)
 
    print(sum(maps[x][y] for x in range(n) for y in range(n)))
cs