돌이 있는 경우를
or이 아닌 and로 조건처리했다가
계속 틀렸었다.
조심,,
# k, s, n 입력
k, s, n = input().split()
n = int(n)
# mapping
mapping = {
'A': 1,
'B': 2,
'C': 3,
'D': 4,
'E': 5,
'F': 6,
'G': 7,
'H': 8,
}
# mapping 이용하여 좌표 반환
kx = mapping[k[0]]
ky = int(k[1])
sx = mapping[s[0]]
sy = int(s[1])
# 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]
# def in_range(x, y):
def in_range(x, y):
return 0 < x and x <= 8 and 0 < y and y <= 8
# dir_num mapping
dir_mapper = {
'R': 0,
'L': 1,
'B': 2,
'T': 3,
'RT': 4,
'LT': 5,
'RB': 6,
'LB': 7,
}
# def move(order):
def move(order):
# 전역 변수 선언
global kx, ky, sx, sy
# 왕의 다음 예상위치
knx, kny = kx + dx[dir_mapper[order]], ky + dy[dir_mapper[order]]
# 왕의 다음 예상 위치가 범위 내에 없으면,
if not in_range(knx, kny):
# 함수 종료
return
# 왕의 다음 예상 위치가 범위 내에 있고, 그 자리에 돌이 없으면,
elif knx != sx or kny != sy:
# 왕만 옮겨줌
kx, ky = knx, kny
# 왕의 다음 예상 위치에 돌이 있으면,
elif knx == sx and kny == sy:
# 돌의 다음 위치 예상
snx, sny = sx + dx[dir_mapper[order]], sy + dy[dir_mapper[order]]
# 돌의 다음 위치가 범위 내에 있으면,
if in_range(snx, sny):
# 돌을 옮기고,
sx, sy = snx, sny
# 왕도 움직임
kx, ky = knx, kny
# 설계
for _ in range(n):
order = input()
move(order)
# 출력용 mapping
rev_mapping = {
1: 'A',
2: 'B',
3: 'C',
4: 'D',
5: 'E',
6: 'F',
7: 'G',
8: 'H',
}
# rev_mapping 이용하여 출력용 제작
k_last, s_last = (rev_mapping[kx]+str(ky)), (rev_mapping[sx]+str(sy))
# 출력
print(k_last)
print(s_last)
'Algorithm(BOJ, Python) > Simulation' 카테고리의 다른 글
| [백준_2174] 로봇 시뮬레이션 python (2) | 2022.09.16 |
|---|---|
| [백준_14499] 주사위굴리기 python (0) | 2022.09.04 |
| [백준_14503] 로봇청소기 python (0) | 2022.08.28 |
| [백준_5215] 지구온난화 python (0) | 2022.08.09 |
| [백준_8911] 거북이 python (0) | 2022.08.03 |