500error
[Cos Pro 1급, Python] 1차 6번 : 체스의 나이트 본문
반응형
문제
체스에서 나이트(knight)는 아래 그림과 같이 동그라미로 표시된 8개의 방향중 한 곳으로 한 번에 이동이 가능합니다.


단, 나이트는 체스판 밖으로는 이동할 수 없습니다.
체스판의 각 칸의 위치는 다음과 같이 표기합니다.

예를 들어, A번줄과 1번줄이 겹치는 부분은 'A1'이라고 합니다.
나이트의 위치 pos가 매개변수로 주어질 때, 나이트를 한 번 움직여서 이동할 수 있는 칸은 몇개인지 return 하도록 solution 함수를 완성해주세요.
코드
def solution(pos):
answer = 0
dx = [1, 1, -1, -1, 2, 2, -2, -2]
dy = [2, -2, -2, 2, 1, -1, -1, 1]
cx = ord(pos[0]) - ord("A")
cy = ord(pos[1]) - ord("0")
for i in range(8) :
nx = cx + dx[i]
ny = cy + dy[i]
if nx >= 0 and nx < 8 and ny >= 0 and ny <= 8:
answer += 1
return answer
pos = "A7"
ret = solution(pos)
print("solution 함수의 반환 값은", ret, "입니다.")
반응형
'알고리즘 > 파이썬' 카테고리의 다른 글
[Cos Pro 1급, Python] 1차 8번 : 누가 당선 되나요 (0) | 2024.02.26 |
---|---|
[Cos Pro 1급, Python] 1차 7번 : 병합 and 정렬 (0) | 2024.02.26 |
[Cos Pro 1급, Python] 1차 5번 : 소용돌이 수 (0) | 2024.02.26 |
[Cos Pro 1급, Python] 1차 4번 : 타임머신 (0) | 2024.02.26 |
[Cos Pro 1급, Python] 1차 3번 : 계산기 by 문자열 (0) | 2024.02.26 |
Comments