삽집하는 개발들/알고리즘

[7일차][프로그래머스][172928]공원산책

악투 2023. 6. 14. 00:16
반응형
def solution(park, routes):
    start_coordinate = []
    blank_coordinate = []
    max_width = len(park[0])
    max_length = len(park)
    
    for idx, data in enumerate(park):        
        for i, val in enumerate(data):  
            if val == 'S':
                start_coordinate.append([
                    idx, i
                ])
                blank_coordinate.append([
                    idx, i
                ])
            elif val == 'X':
                blank_coordinate.append([])
            else:
                blank_coordinate.append([
                    idx, i
                ])
                
    for data in routes:
        direction, step = data.split(' ')[0], data.split(' ')[1]
        
        if direction == 'E':
            if start_coordinate[0][1] + int(step) >= max_width:       
                pass
            
            previous_coordinate = start_coordinate[0]
            for idx in range(int(step)):     
                start_coordinate[0] = [start_coordinate[0][0], int(start_coordinate[0][1]) + 1]               
                if start_coordinate[0] not in blank_coordinate:
                    start_coordinate[0] = previous_coordinate
                    break
           
        elif direction == 'S':               
            if start_coordinate[0][0] + int(step) >= max_length:                
                pass  
                            
            previous_coordinate = start_coordinate[0]
            for idx in range(int(step)):  
                start_coordinate[0] = [start_coordinate[0][0] + 1, int(start_coordinate[0][1])] 
                if start_coordinate[0] not in blank_coordinate:                    
                    start_coordinate[0] = previous_coordinate                   
                    break
            
        elif direction == 'W':                    
            if start_coordinate[0][1] + int(step) > max_width:                
                pass
          
            previous_coordinate = start_coordinate[0]
            
            for idx in range(int(step)):     
                start_coordinate[0] = [start_coordinate[0][0], int(start_coordinate[0][1]) - 1]    
                if start_coordinate[0] not in blank_coordinate:
                    print("not in", start_coordinate[0])
                    start_coordinate[0] = previous_coordinate
                    break
                    
        else:
            if start_coordinate[0][0] + int(step) > max_length:                
                pass
            
            previous_coordinate = start_coordinate[0]
            for idx in range(int(step)):     
                start_coordinate[0] = [int(start_coordinate[0][0]) - 1, int(start_coordinate[0][1])]            
                if start_coordinate[0] not in blank_coordinate:
                    start_coordinate[0] = previous_coordinate
                    break
                    
    return start_coordinate[0]
def solution(park, routes):
    max_w = len(park[0])
    max_h = len(park)
    start_point = []
    coordinate_obj = {"N" : [-1, 0],  "E": [0, 1],  "S" : [1, 0], "W" : [0, -1]}
    
    for i in range(max_h):
        for j in range(max_w):
            if park[i][j] == "S":
                start_point = [i, j]         
    
    for route in routes:
        direction, step = route.split(' ')
        x, y = coordinate_obj[direction]
        next_x, next_y = start_point[0],  start_point[1]
        
        for idx in range(int(step)):            
            next_x, next_y = int(x + next_x), int(y + next_y)  
            
            if 0 <= next_x < max_h and 0 <= next_y < max_w:
                if park[next_x][next_y] == "X":
                    break
        
        if 0 <= next_x < max_h and 0 <= next_y < max_w:   
             if park[next_x][next_y] != "X":                    
                start_point = [next_x, next_y]

    
    return start_point
반응형