문제 접근(문제 분석 → 풀이 아이디어)
코드를 풀이할 때 적었던 플로우가 있나요?
"""
<https://www.acmicpc.net/problem/16509>
#
- 궁성 위치 : (0.3)&(2.5)/ (7, 3)과 (9, 5) => 왕 위치 (8,4)
- (10,9) 크기 fied
- 상 <이동 방법>: 상하좌우1칸+ 같은 방향 대각선 2칸
2시 : (우+상)x2 | -1 +1
5시 : (우+하)x2 | +1 +1
8시 :(좌+하)x2 |
11시 : (좌+상)x2
=> 상 -> 11시 , 2시 / 하 -> 8시 , 5시 /
- <이동 가능 조건>
(1)이동 경로에 다른 기물에 존재하지 않을 때 이동 가능
(2) field 안에 존재
goal) 상 -> 왕 도달하는 최소 이동 횟수 (BFS)
#flow
1. 상& 왕(실제 좌표 위치로 변환) 위치 좌표 + field 값
2. 이동 가능한 상황_ 3점 모두 비어져있으면 이동 가능
3점 check 하기 (1) 상하좌우 / (2)대각선 1회 / (2)대각선 2회
상 - 우상 , 좌상
하 - 좌하 , 우하
좌 - 좌상, 좌하
우 - 우상, 우하
# 1st erro
- ny1, nx1 = cy +dy[i%2] , cx+dx[i%2]
ny2, nx2 = ny1 + dl[dl_combined[i]][0] , nx1 + dl[dl_combined[i]][1]
ny3 , nx3 = ny2 + dl[dl_combined[i]][0] , nx2 + dl[dl_combined[i]][1]
=> i는 0~8 번으로 상 + 우상/좌상 // 하 + 좌하/우하 // 좌 + 좌상,좌하 // 우 + 우상, 우하 순으로 진행
"""
import sys
from collections import deque
INF = 1e9
# 상하좌우
dy = [-1,1,0,0]
dx = [0,0,-1,1]
dl= [[-1,1],[1,1],[1,-1],[-1,-1]] # 우상 . 우하 / 좌하 / 좌상
dl_combined = [0,3,1,2,2,3,0,1]
sy ,sx= map(int,sys.stdin.readline().split())
ty,tx = map(int, sys.stdin.readline().split())
field = [[INF]*9 for _ in range(10)]
field[sy][sx] = 0
field[ty][tx] = -1
q = deque()
q.append([sy,sx])
def check_bound(y,x) :
if 0<= y <=9 and 0<= x <=8 and field[y][x]!=-1 : # 기물 x , field 안에 , 방문 상관x => 왕 위치만 안됨x
return True
return False
def check_bound2(y,x) :
if 0<= y <=9 and 0<= x <=8 : # 기물 가능
return True
return False
while q :
cy,cx = q.popleft()
for i in range(8):
ny1, nx1 = cy +dy[i//2] , cx+dx[i//2]
ny2, nx2 = ny1 + dl[dl_combined[i]][0] , nx1 + dl[dl_combined[i]][1]
ny3 , nx3 = ny2 + dl[dl_combined[i]][0] , nx2 + dl[dl_combined[i]][1]
if check_bound(ny1, nx1) and check_bound(ny2, nx2) and check_bound2(ny3, nx3) :
if ny3 == ty and nx3 == tx :
# print(f"succes====")
print(field[cy][cx] +1)
exit()
elif field[cy][cx] +1 <= field[ny3][nx3] :
field[ny3][nx3] =field[cy][cx] +1 # 업데이트
# print(f"{cy}{cx} -> {i}: {ny1}{nx1} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")
q.append([ny3,nx3])
# print(f"{cy}{cx} -> {i}: {ny1}{nx2} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")
print(field[ty][tx])
