Algorithm/Implementation

[백준 20057번] 마법사 상어와 토네이도

킹우현 2023. 9. 7. 11:41

 

20057번: 마법사 상어와 토네이도

마법사 상어가 토네이도를 배웠고, 오늘은 토네이도를 크기가 N×N인 격자로 나누어진 모래밭에서 연습하려고 한다. 위치 (r, c)는 격자의 r행 c열을 의미하고, A[r][c]는 (r, c)에 있는 모래의 양을

www.acmicpc.net

n = int(input())

area = [list(map(int,input().split())) for _ in range(n)]

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

count = 0
length = 1
direction = 0

answer = 0

x, y = n//2, n//2

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/100)
    two_percent = int(total*2/100)

    a_x, a_y = x + dx[d], y + dy[d]
    b_x, b_y = x - dx[d], y - dy[d]
    five_x, five_y = x + dx[d]*2, y + dy[d]*2
    
    if d == 0 or d == 2: # 왼쪽이나 오른쪽일 경우
        ten_list = [(a_x-1,a_y), (a_x+1,a_y)]
        seven_list = [(x-1,y),(x+1,y)]
        two_list= [(x-2,y),(x+2,y)]
        one_list = [(b_x-1,b_y),(b_x+1,b_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)]
        two_list = [(x,y-2),(x,y+2)]
        one_list = [(b_x,b_y-1),(b_x,b_y+1)]
    
    if 0 <= five_x < n and 0 <= five_y < n:
        area[five_x][five_y] += five_percent
        total -= five_percent
    else:
        answer += five_percent
        total -= five_percent
    
    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
            total -= ten_percent
        else:
            answer += ten_percent
            total -= ten_percent
    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
            total -= seven_percent
        else:
            answer += seven_percent
            total -= seven_percent
    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
            total -= two_percent
        else:
            answer += two_percent
            total -= two_percent
    
    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
            total -= one_percent
        else:
            answer += one_percent
            total -= one_percent
    
    if 0 <= a_x < n and 0 <= a_y < n:
        area[a_x][a_y] += total
    else:
        answer += total

    area[x][y] = 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)

이번 문제는 주어진 N x N 좌표의 가운데 칸부터 시작하여 반시계 방향으로 토네이도가 움직인다고 가정할 때, 토네이도가 이동하면서 모래르 흩날릴 때 좌표 밖으로 나간 모래의 양을 구하는 문제이다.

 

문제를 풀기 위해서 먼저 while 문과 for문을 사용하여 가운데칸부터 (0,0) 칸까지 좌표를 구하였고, 토네이도가 이동할 때 모래를 흩날리는 함수(move)를 구현하여 간단하게 해결할 수 있었다.

 

구현과 시뮬레이션을 연습해볼 수 있었던 간단한 문제였다 :)