[백준] 1331번 나이트 투어

거북이·2023년 1월 8일
0

백준[실버5]

목록 보기
81/114
post-thumbnail

💡문제접근

①. x축 방향으로 ±2만큼 이동하고 y축 방향으로 ±1만큼 이동
②. x축 방향으로 ±1만큼 이동하고 y축 방향으로 ±2만큼 이동
③. 한 번 방문했던 지점은 재방문이 불가하다.

💡코드(메모리 : 30616KB, 시간 : 36ms)

visited = []
ans = "Valid"
for i in range(36):
    if i == 0:
        start = input()
        start_location = start
        visited.append(start)
    elif i < 35:
        now_location = input()
        if (abs(ord(start_location[0]) - ord(now_location[0])) == 2 and abs(int(start_location[1]) - int(now_location[1])) == 1) and now_location not in visited:
            pass
        elif (abs(ord(start_location[0]) - ord(now_location[0])) == 1 and abs(int(start_location[1]) - int(now_location[1])) == 2) and now_location not in visited:
            pass
        else:
            ans = "Invalid"
        visited.append(now_location)
        start_location = now_location
    else:
        now_location = input()
        if (abs(ord(now_location[0]) - ord(start[0])) == 2 and abs(int(now_location[1]) - int(start[1])) == 1) and now_location not in visited:
            pass
        elif (abs(ord(now_location[0]) - ord(start[0])) == 1 and abs(int(now_location[1]) - int(start[1])) == 2) and now_location not in visited:
            pass
        else:
            ans = "Invalid"
        visited.append(now_location)

visited = list(set(visited))	# 중복되는 요소를 제거
if len(visited) == 35:			
    print(ans)
else:
    print(ans)

💡소요시간 : 1h

0개의 댓글