구현 : 왕실의나이트

주리·2022년 11월 10일
0

코테_구현

목록 보기
3/4
post-thumbnail

변수

  1. 입력받는 변수 (x,y)
  2. List X [1,-1,0,0]
    List Y [0,0,1,-1]
    R L U D
  3. 이동할 수 있는 경우 8가지
    X Y
  • RRU 2 1
  • RRD 2 -1
  • LLU -2 1
  • LLD -2 -1
  • UUR 1 2
  • UUL -1 2
  • DDR 1 -2
  • DDL -1 -2
  1. result

로직

  1. xy를 입력받는다
  • x 가 a 면 1~
  1. 8번 for문 돌면서
    xy를 리스트 X[n] 을 x에 더하면서 8보다 작으면 result +=1

코드

  • 입력받는 곳에서 고민해야 할 점
    : xy를 그냥 한번에 (a1) 입력받으려고 했는데, 띄어쓰기와 같은 split할 구간이 없었다
    -> str로 입력받고 , [0][1]로 나누어주었다,,
  • a~h로 입력받은 x를 if문 없이 1~8로 바꾸어줄 수 있는지
  • 여기서는 겹치지 않지만, 겹치는 자리가 생긴다면 그건 어떻게 구분해줄지
#x,y=input().split()
test=input()
x=test[0]
y=int(test[1])

X = [2,2,-2,-2,1,-1,1,-1]
Y = [1,-1,1,-1,2,2,-2,-2]
result =0

if (x=='a'):
  x=1
elif(x=='b'):
  x=2
elif(x=='c'):
  x=3
elif(x=='d'):
  x=4
elif(x=='e'):
  x=5
elif(x=='f'):
  x=6
elif(x=='g'):
  x=7
elif(x=='h'):
  x=8

x2=x
y2=y

for i in range(8):
  x=x2
  y=y2
  #print('x2',x)
  #print('y2',y)
  
  x += X[i]
  y += Y[i]
  
  if(x>0 and x <9 and y>0 and y<9) :
    #print('x',x)
    #print('y',y)
    result+=1

print('result',result)

다시 푼 코드

  • 아스키코드 값을 가져오는 ord() 함수를 사용
    -> x값을 int로 받아오기 (+1은 젤 처음 a의 위치)
  • dx , dy 를 이용
  • realx realy 없이 하는 방법이 있을까?
where = input()
y = int(where[1])
x = int(ord(where[0])) - int(ord('a')) +1

dx = [2,2,-2,-2,1,-1,1,-1]
dy = [1,-1,1,-1,2,2,-2,-2]

result=0
realx=x
realy=y

for i in range(8):
  x=realx
  y=realy
  
  x += dx[i]
  y += dy[i]
  if (x>0 and x<8 and y>0 and y<8):
    result += 1

print(result)
profile
완벽한 글 보다, 그 과정들을 기록하는 개발자

0개의 댓글