Algorithm/Implementation

[프로그래머스] 공원 산책

킹우현 2023. 6. 23. 16:24

def solution(park, routes):
    
    row_l = len(park)
    col_l = len(park[0])
    
    start_location = (0,0)
    
    route_list = []
    
    area = [[""]*col_l for _ in range(row_l)]
    
    for i,row in enumerate(park):
        for j,item in enumerate(row):
            area[i][j] = item
            if item == "S":
                start_location = [i,j]
    
    for i in routes:
        route_list.append((i[0],int(i[2])))
        
    for i in route_list:
        d, n = i[0], i[1]
        exist_x = False
        
        if d == "N":
            if (start_location[0] - n) >= 0:
                for j in range(start_location[0]-n,start_location[0]):
                    if area[j][start_location[1]] == "X":
                        exist_x = True
                if not exist_x:
                    start_location[0] -= n
        elif d == "S":
            if (start_location[0] + n) < row_l:
                for j in range(start_location[0],start_location[0]+n+1):
                    if area[j][start_location[1]] == "X":
                        exist_x = True
                if not exist_x:
                    start_location[0] += n
        elif d == "W":
            if (start_location[1] - n) >= 0:
                for j in range(start_location[1]-n,start_location[1]):
                    if area[start_location[0]][j] == "X":
                        exist_x = True
                if not exist_x:   
                    start_location[1] -= n
        elif d == "E":
            if (start_location[1] + n) < col_l:
                for j in range(start_location[1],start_location[1]+n+1):
                    if area[start_location[0]][j] == "X":
                        exist_x = True
                if not exist_x:
                    start_location[1] += n
    
    return start_location

이번 문제는 출발 지점으로부터 routes에 저장된 순서대로 로봇 강아지를 움직이되, 이동한 좌표가 공원(area)를 벗어나는지이동하는 과정에서 장애물이 존재하는지 조사를 해주어야 하는 문제이다.

 

단순한 구현 문제이지만, 문제에서 주어진 조건을 정확히 파악하는 연습을 할 수 있던 문제이다 :)