[백준] 최소 힙

[백준] 최소 힙


풀이

로봇이 가는 방향과 현재 판화의 상태에 따라 결과가 바뀌는 문제이다.
방향과 상태를 나타내는 배열을 만들고 행동하는 것에 따라 알고리즘을 블록화 해서 상태를 더 명확히 나타내야 한다.

from sys import stdin
input = lambda : stdin.readline().rstrip()

n = int(input())
move = list(map(str, input()))

# 로봇 팔이 어느 한 점에 위치할 때 상태를 나타낸다.
shape = [chr(46), chr(124), chr(45), chr(43)]
# 로봇 팔이 가는 방향
direct = {'D':[1, 0], 'U':[-1, 0], 'R':[0, 1], 'L':[0, -1]}
graph = [[shape[0]] * n for _ in range(n)]

#판화의 현 상태에 따라 변화
def change_graph(move, value):
    output = None
    if move == 'D' or move == 'U':
        output = shape[1]
        if value == shape[2]:
            output = shape[3]
        elif value == shape[3]:
            output = shape[3]
    elif move == 'R' or move == 'L':
        output = shape[2]
        if value == shape[1]:
            output = shape[3]
        elif value == shape[3]:
            output = shape[3]
    return output

x, y = 0, 0
for i in range(len(move)):

    if move[i] == 'D':
        dx,dy = direct['D']
    elif move[i] == 'U':
        dx,dy = direct['U']
    elif move[i] == 'R':
        dx,dy = direct['R']
    elif move[i] == 'L':
        dx,dy = direct['L']

    # 판화 밖으로 나갈 때
    if 0<= x+dx <n and 0<= y+dy <n:
        graph[x][y] = change_graph(move[i], graph[x][y])
        graph[x+dx][y+dy] = change_graph(move[i], graph[x+dx][y+dy])

        x = x+dx
        y = y+dy

# 출력
for i in range(n):
    print("".join(graph[i]))

실수 및 배운 점

  • 출력할 때 graph로 바로 나타내서 틀리게 되었다. 다시 풀어주는 것을 잊지 말자!