구현 - 왕실의 나이트

Giho Kim·2023년 10월 11일

코테 연습

목록 보기
17/26

왕실의 나이트

내풀이

start_point = input()
w, l = start_point[0], start_point[1]

width = ['a','b','c','d','e','f','g','i']
move_lst = [[-2, 1], [-2, -1],[2, 1], [2, -1], [1, -2],[-1, -2], [1, 2],[-1, 2]]

w = width.index(w)
l = int(l) - 1
cnt = 0
for i in move_lst:
    moved_w = w + i[0]
    moved_l = l + i[1]
    
    if (7 >= moved_w >= 0) and (7 >= moved_l >= 0):
        cnt += 1
print(cnt)
  • 풀이와 거의 비슷하게 풀었다.
  • width, length라고 하지말고 row col로 했으면 조금더 가독성이 좋을꺼 같다.
  • ord function을 썻다면 훨씬 메모리 적으로 좋았을꺼 같다.

책풀이

input_date = input()
row = int(input_data[1])
col = int(ord(input_data[0]) - int(ord('a')) + 1

steps = [[-2, 1], [-2, -1],[2, 1], [2, -1], [1, -2],[-1, -2], [1, 2],[-1, 2]]

result = 0
for step in steps:
	next_row = row + step[0]
    next_col = col + step[1]
    
    if next_row >= 1 and next_row <= 8 and next_col >= 1 and next_col <= 8:
    result += 1
    
print(result)
profile
취준돌이 개발자 김기호

0개의 댓글