최대 1 분 소요

문제 링크

프로그래머스 - 공원산책

문제 풀이

DIR = 0
DIST = 2

def findStart(park):
    for i in range(len(park)):
        for j in range(len(park[0])):
            if park[i][j] == 'S':
                return (i,j)
    return (0,0)
def direcConvertor(direc):
    y_change=0
    x_change=0
    if direc=="N":
        y_change= -1
        x_change=0
    elif direc=="E":
        y_change=0
        x_change=1
    elif direc=="S":
        y_change=1
        x_change=0
    else:
        y_change=0
        x_change=-1
    return (y_change,x_change)
        
def solution(park, routes):
    answer = []
    start = findStart(park)
    
    y = start[0]
    x = start[1]
    
    for route in routes:
        direc = route[DIR]
        dist = route[DIST]
        
        y_change,x_change = direcConvertor(direc)
        
        yCopy = y
        xCopy = x
        yLen = len(park)
        xLen = len(park[0])
        i=0
        while i<int(dist):
            yCopy = yCopy + y_change
            xCopy = xCopy + x_change
            if not(yCopy>=0 and yCopy<yLen and xCopy>=0 and xCopy<xLen and park[yCopy][xCopy] != "X"):
                break
            i=i+1
        if i>=int(dist):
            y=yCopy
            x=xCopy
    
    answer = [y,x]
    
    return answer

댓글남기기