■코테 기출문제/Summer,Winter Coding(~2018)
[Level 3] 방문 길이
영민 박
2020. 5. 8. 20:00
문제 : https://programmers.co.kr/learn/courses/30/lessons/49994
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
▶ 코드
import copy
def solution(dirs):
path = [] # 지나간 길 ex) [0,0,1,0] : (0,0) -> (1,0)
last = [0,0]
for a in dirs:
temp = copy.deepcopy(last)
if a == 'U':
last[1] +=1
if last[1] <= 5 and last[1] >= -5:
if (temp + last) not in path and (last + temp) not in path:
path.append(temp + last)
else:
last[1] -=1 # 범위 넘었을 경우 이동경로 다시 되돌려주기
elif a == 'D':
last[1] -=1
if last[1] <= 5 and last[1] >= -5:
if (temp + last) not in path and (last + temp) not in path:
path.append(temp + last)
else:
last[1] +=1 # 범위 넘었을 경우 이동경로 다시 되돌려주기
elif a == 'R':
last[0] +=1
if last[0] <= 5 and last[0] >= -5:
if (temp + last) not in path and (last + temp) not in path:
path.append(temp + last)
else:
last[0] -=1 # 범위 넘었을 경우 이동경로 다시 되돌려주기
elif a == 'L':
last[0] -=1
if last[0] <= 5 and last[0] >= -5:
if (temp + last) not in path and (last + temp) not in path:
path.append(temp + last)
else:
last[0] += 1 # 범위 넘었을 경우 이동경로 다시 되돌려주기
#print(last)
#print(path)
#print(len(path))
return len(path)
#solution("ULURRDLLU")
#solution("LULLLLLLU")
#solution("LR") # 1이 나와야 한다.
#solution("L")