king,rock, t = input().split()
t = int(t)
move = [input() for _ in range(t)]
king_x = int(ord(king[0])) - int(ord('A')) + 1
king_y = int(king[1])
rock_x = int(ord(rock[0])) - int(ord('A')) + 1
rock_y = int(rock[1])
alp = ['A','B','C','D','E','F','G','H']
move_type = ['R','L' ,'B' ,'T' ,'RT' ,'LT' ,'RB' ,'LB']
dx = [+1,-1, 0, 0, +1, -1, +1, -1]
dy = [0, 0, -1, +1, +1, +1, -1, -1]
for i in range(t): # t번 반복
# move[i]가 move_type에서 몇번째 인덱스인지 찾기
index = move_type.index(move[i])
# 킹이 t번 움직인다. (예외 검사)
if king_x + dx[index] >= 1 and king_x + dx[index] <= 8 and king_y + dy[index] >= 1 and king_y + dy[index] <= 8: # 킹 예외
if king_x + dx[index] == rock_x and king_y + dy[index] == rock_y: #돌에 얹힘
if rock_x + dx[index] >= 1 and rock_x + dx[index] <= 8 and rock_y + dy[index] >= 1 and rock_y + dy[index] <= 8: # 돌예외
king_x += dx[index]
king_y += dy[index]
rock_x += dx[index]
rock_y += dy[index]
else:
continue
else :
king_x += dx[index]
king_y += dy[index]
else:
continue
print(alp[king_x-1]+str(king_y))
print(alp[rock_x-1]+str(rock_y))
딕셔너리를 사용한 코드가 훨씬 깔끔한 것 같다.
딕셔너리는 key와 value로 이루어진다
grade = {'pey': 10, 'julliet': 99}
grade['pey']
->10
grade['julliet']
-> 99
else, continue를 반드시 쓸 필요는 없다.
input을 꼭 처음에 다 받을 필요는 없다.
map 함수 작동 원리를 좀 더 정리 할 필요가 있는 것 같다...
king, stone, N = input().split()
k = list(map(int, [ord(king[0]) - 64, king[1]]))
s = list(map(int, [ord(stone[0]) - 64, stone[1]]))
#이때 k와 s는 [1,1] [8,8]
# 딕셔너리 (이동 타입에 따라 dx와 dy 설정)
move = {'R': [1, 0], 'L': [-1, 0], 'B': [0, -1], 'T': [0, 1], 'RT': [1, 1], 'LT': [-1, 1], 'RB': [1, -1], 'LB': [-1, -1]}
# 움직이는 횟수 만큼 실행
for _ in range(int(N)):
m = input() #지금 이동
# 움직였을 경우의 위치 : nx, ny
nx = k[0] + move[m][0]
ny = k[1] + move[m][1]
# 킹 조건 검사
if 0 < nx <= 8 and 0 < ny <= 8:
# 돌 위에 얹히는가?
if nx == s[0] and ny == s[1]:
sx = s[0] + move[m][0]
sy = s[1] + move[m][1]
# 돌 조건 검사
if 0 < sx <= 8 and 0 < sy <= 8:
k = [nx, ny] # 킹 이동
s = [sx, sy] # 돌 이동
else:
k = [nx, ny] # 킹만 이동
print(f'{chr(k[0] + 64)}{k[1]}')
print(f'{chr(s[0] + 64)}{s[1]}')