본문 바로가기

코딩 테스트/프로그래머스

[프로그래머스] 방문 길이

 

코드 설계

  • 조건 1 : 범위를 넘어가지 않는 경우
  • 조건 2 : 방향 고려 x
  • 현재 좌표는 전역 변수로 설정

 

정답 코드

x = 0
y = 0

# 경계를 넘어가는지
def isBoundary(x,y) :
    if x < -5 or x > 5 or y < -5 or y > 5 :
        return False
    return True

# 좌표 값 이동
def move(com) :
    global x, y
    
    if com == 'U' : # 위
        if isBoundary(x, y+1):
            return x, y+1
        
    if com == "D" : # 아래
        if isBoundary(x, y-1):
            return x, y-1
        
    if com == "R" : # 오
        if isBoundary(x+1, y):
            return x+1, y
    
    if com == "L" : # 왼
        if isBoundary(x-1, y):
            return x-1, y
    
    return x, y # 범위를 벗어난다면 처음 값 return

        
def solution(dirs):
    global x, y
    
    ans = 0
    went = [(x,y)]
    
    for dir in dirs :
        nx, ny = move(dir)
        
        if nx == x and ny == y : # 범위를 벗어나 이전과 같다면 다음 방향 명령어
            continue
            
        if ((x, y, nx, ny) not in went) :
            if ((nx, ny, x, y) not in went) : # 방향 상관 x
                went.append((x, y, nx, ny))
                ans += 1
            
        x = nx
        y = ny
        
    return ans

 

다른 정답 코드 1

def solution(dirs):
    s = set()
    d = {'U': (0,1), 'D': (0, -1), 'R': (1, 0), 'L': (-1, 0)}
    x, y = 0, 0
    for i in dirs:
        nx, ny = x + d[i][0], y + d[i][1]
        if -5 <= nx <= 5 and -5 <= ny <= 5:
            s.add((x,y,nx,ny))
            s.add((nx,ny,x,y))
            x, y = nx, ny
    return len(s)//2

 

다른 정답 코드 2

def solution(dirs):

    walk_list = []
    start_location = [0,0]
    now_location = [0,0]
    for c in dirs:
        if c == 'U' and start_location[1] < 5:
            now_location = [start_location[0],start_location[1] + 1]

        elif c == 'L' and start_location[0] > -5:
            now_location = [start_location[0] - 1, start_location[1]]

        elif c == 'R' and start_location[0] < 5:
            now_location = [start_location[0] + 1, start_location[1]]

        elif c == 'D'and start_location[1] > -5:
            now_location = [start_location[0], start_location[1] - 1]


        if ([start_location,now_location] not in walk_list) and ([now_location,start_location] not in walk_list) \
                and (now_location != start_location) :
            walk_list.append([start_location,now_location])

        start_location = now_location

    return len(walk_list)

 

다른 정답 코드 3

def solution(dirs):
    point = [0,0]
    answer = set()

    for _dirs in dirs:
        if _dirs == 'U' and point[1]!=5:
            point[1]+=1
            nn = (point[0], point[1]-.5)
        elif _dirs == 'D' and point[1]!=-5:
            point[1]+=-1
            nn = (point[0], point[1]+.5)
        elif _dirs == 'R' and point[0]!=5:
            point[0]+=1
            nn = (point[0]-.5, point[1])
        elif _dirs == 'L' and point[0]!=-5:
            point[0]+=-1
            nn = (point[0]+.5, point[1])
        else: pass

        answer.add(nn)

    return len(answer)