programmers | 공원 산책

아빠늑대·2023년 4월 8일
0

programmers | 공원 산책


global width, height, x, y, axis, direction, count


def sign(num):
    if num >= 0:
        return 1
    return -1


def hasRock(park):
    global x, y, axis, count
    s = sign(count)
    abscount = abs(count)

    if axis == 'x':
        for d in range(abscount + 1):
            if park[y][x + s * d] == 'X':
                return True
    elif axis == 'y':
        for d in range(abscount + 1):
            if park[y + s * d][x] == 'X':
                return True
    return False


def Move(park, route):
    global width, height, x, y, axis, direction, count


    if axis == 'x':
        dest = x + count
        if dest < 0 or width <= dest:
            return False
        elif hasRock(park) == True:
            return False
        x += count
    elif axis == 'y':
        dest = y + count
        if dest < 0 or height <= dest:
            return False
        elif hasRock(park) == True:
            return False
        y += count
    return True

def solution(park, routes):
    global width, height, x, y, axis, direction, count

    width, height = len(park[0]), len(park)

    for i in range(height):
        for j in range(width):
            if park[i][j] == 'S':
                y, x = i, j
                break
    
    for route in routes:
        direction = route[0:1]
        count = int(route[1:])
        if direction == 'W' or direction == 'N':
            count *= -1
        if direction == 'E' or direction == 'W':
            axis = 'x'
        else:
            axis = 'y'
        Move(park, route)

    return [y, x]

# park00 = ["SOO","OOO","OOO"]
# routes00 = ["E 2","S 2","W 1"]
# print(solution(park00, routes00))

# park01 = ["SOO","OXX","OOO"]
# routes01 = ["E 2","S 2","W 1"]
# print(solution(park01, routes01))

# park02 = ["OSO","OOO","OXO","OOO"]	
# routes02 = ["E 2","S 3","W 1"]
# print(solution(park02, routes02))
  • 다른 사람 풀이를 보니..
    • dictionary로 방향을 지정하네요
  • range에 +1 해주는거 찾는데 오래걸림
  • 나머지는 기계적으로 푼듯...
profile
두괄식 게으른 완벽주의자

0개의 댓글