🔻PS/Codetree

[Codetree] 숫자가 더 큰 인접한 곳으로 이동 Python

_니지 2024. 9. 2. 21:51

https://www.codetree.ai/missions/2/problems/move-to-larger-adjacent-cell/description

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

import sys

input = sys.stdin.readline

n, x, y = map(int, input().rstrip().split(" "))

x -= 1
y -= 1

graph = [] 
answer = []

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

for _ in range(n):
    temp = list(map(int, input().rstrip().split(" ")))
    graph.append(temp)


### 구현부 ###
def simul():
    global x, y

    for i in range(4):
        nx = x + dx[i]
        ny = y+ dy[i]

        if 0 <= nx and nx < n and 0<= ny and ny < n:
            if graph[nx][ny] > graph[x][y]:
                # answer.append(graph[nx][ny])
                x = nx
                y = ny
                return True
    return False

answer.append(graph[x][y])
while simul():
    answer.append(graph[x][y])

print(*answer)
728x90
반응형