Study/BAEKJOON
[BAEKJOON] 20057번: 마법사 상어와 토네이도
줴림
2025. 4. 8. 02:14
이름 구려
DFS도 BFS도 아닌 시뮬레이션 문제. 처음 풀어보는 유형이라 그냥 진짜 부딪쳐 보는 수밖에 없다.
다른 사람의 코드를 하나하나 뜯어보면서 어떤 알고리즘인지 학습했다.
def move(x, y, d):
global answer
total = area[x][y]
five_percent = int(total*5/100)
ten_percent = int(total*10/100)
seven_percent = int(total*7/100)
one_percent = int(total*1/100)
two_percent = int(total*2/100)
a_x, a_y = x+dx[d], y+dy[d] # 앞쪽 1칸
b_x, b_y = x-dx[d], y-dy[d] # 뒤쪽 1칸
five_x, five_y = x+2*dx[d], y+2*dy[d] # 앞쪽 2칸
if d==0 or d==2: # 왼쪽 or 오른쪽
ten_list = [(a_x+1, a_y), (a_x-1, a_y)]
seven_list = [(x+1, y), (x-1, y)]
one_list = [(b_x+1, b_y), (b_x-1, b_y)]
two_list = [(x+2, y), (x-2, y)]
elif d==1 or d==3:
ten_list = [(a_x, a_y+1), (a_x, a_y-1)]
seven_list = [(x, y+1), (x, y-1)]
one_list = [(b_x, b_y+1), (b_x, b_y-1)]
two_list = [(x, y+2), (x, y-2)]
# 5% 구역
if (0 <= five_x < n) and (0 <= five_y < n):
area[five_x][five_y] += five_percent
else:
answer += five_percent
total -= five_percent
# 10% 구역
for (ten_x, ten_y) in ten_list:
if (0 <= ten_x < n) and (0 <= ten_y < n):
area[ten_x][ten_y] += ten_percent
else:
answer += ten_percent
total -= ten_percent
# 7% 구역
for (seven_x, seven_y) in seven_list:
if (0 <= seven_x < n) and (0 <= seven_y < n):
area[seven_x][seven_y] += seven_percent
else:
answer += seven_percent
total -= seven_percent
# 1% 구역
for (one_x, one_y) in one_list:
if (0 <= one_x < n) and (0 <= one_y < n):
area[one_x][one_y] += one_percent
else:
answer += one_percent
total -= one_percent
# 2% 구역
for (two_x, two_y) in two_list:
if (0 <= two_x < n) and (0 <= two_y < n):
area[two_x][two_y] += two_percent
else:
answer += two_percent
total -= two_percent
# 알파 구역: 남은 모래
if (0 <= a_x < n) and (0 <= a_y < n):
area[a_x][a_y] += total
else:
answer += total
area[x][y] = 0
n = int(input())
area = [list(map(int, input().split())) for _ in range(n)]
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
x = n // 2 # 시작 x좌표
y = n // 2 # 시작 y좌표
count = 0
length = 1 # 이동 칸 수
direction = 0 # dx, dy 인덱스
answer = 0
while (0<=x<n) and (0<=y<n):
count += 1
for i in range(length):
x += dx[direction]
y += dy[direction]
if (0<=x<n) and (0<=y<n):
move(x, y, direction)
direction = (direction + 1) % 4
if count == 2:
count = 0
length += 1
print(answer)