반응형
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반응형
'삽집하는 개발들 > 알고리즘' 카테고리의 다른 글
| [9일차][프로그래머스][2023 카카오 - 150370]개인정보 수집 유효성 검사 (0) | 2023.06.19 |
|---|---|
| [8일차][프로그래머스][178871]달리기 경주 (0) | 2023.06.16 |
| [6일차] [프로그래머스] [2022 카카오 - 92334]신고 결과 받기 (0) | 2023.06.12 |
| [5일차] [CodeUp] [기초 - 반복실행구조]basic_repetitive_execution (1) | 2023.06.11 |
| [4일차][프로그래머스][181938]두 수의 연산값 비교하기 (0) | 2023.06.09 |