Algorithm/이코테
[이코테] 왕실의 나이트 / 파이썬 / python / 구현
uni2237
2021. 8. 24. 17:51
728x90
728x90
👩🏻💻 code
import sys
sys.stdin = open("input.txt","rt")
input = sys.stdin.readline
dx=[-2,-2,2,2,-1,1,-1,1]
dy=[-1,1,-1,1,-2,-2,2,2]
#ord() : 문자 -> 숫자
#chr(): 숫자 -> 문자
where=input()
x=ord(where[0])-ord('a')
y=int(where[1])-1
count=0
for i in range(len(dx)):
nx,ny=x+dx[i],y+dy[i]
if 0<=nx<8 and 0<=ny<8:
count+=1
print(count)
# dx,dy 말고 순서쌍 방법으로 하면?
steps=[(-2,-1),(-2,+1),(+2,-1),(+2,+1),(-1,-2),(-1,+2),(+1,-2),(+1,+2)]
for step in steps:
nx=x+step[0]
ny=y+step[1]
if nx>0 and nx<8 and ny>0 and ny<8 :
count+=1
print(count)
🌈 해설
나이트가 움직일 수 있는 방향을 미리 dx,dy배열로 구현해둔 다음 사용하기!
ord 사용해서 a를 0으로 만들어주기
728x90
728x90