Skip to content

Commit 8165038

Browse files
committed
[BOJ] #16509. 장군 / 골드5 / 60분 / 성공
1 parent 88c9f7c commit 8165038

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
# 1. 상과 왕 초기 위치
7+
s_r, s_c = map(int, input().split()) # 상의 위치
8+
k_r, k_c = map(int, input().split()) # 왕의 위치
9+
10+
# 장기판 (10x9)
11+
grid = [[0 for _ in range(9)] for _ in range(10)]
12+
visited = [[False for _ in range(9)] for _ in range(10)]
13+
14+
# 8가지 이동 경로
15+
directions = [
16+
# 상
17+
((-1, 0), (-1, 1), (-1, 1)), # 오른쪽 위
18+
((-1, 0), (-1, -1), (-1, -1)), # 왼쪽 위
19+
# 하
20+
((1, 0), (1, 1), (1, 1)), # 오른쪽 아래
21+
((1, 0), (1, -1), (1, -1)), # 왼쪽 아래
22+
# 좌
23+
((0, -1), (-1, -1), (-1, -1)), # 왼쪽 위
24+
((0, -1), (1, -1), (1, -1)), # 왼쪽 아래
25+
# 우
26+
((0, 1), (-1, 1), (-1, 1)), # 오른쪽 위
27+
((0, 1), (1, 1), (1, 1)) # 오른쪽 아래
28+
]
29+
30+
31+
def bfs():
32+
queue = deque([(s_r, s_c)]) # 상 위치부터 시작
33+
34+
while queue:
35+
r, c = queue.popleft()
36+
37+
# 왕에게 도달한 경우
38+
if (r, c) == (k_r, k_c):
39+
return grid[r][c]
40+
41+
for d1, d2, d3 in directions:
42+
nr1, nc1 = r + d1[0], c + d1[1]
43+
nr2, nc2 = nr1 + d2[0], nc1 + d2[1]
44+
nr3, nc3 = nr2 + d3[0], nc2 + d3[1]
45+
46+
if (0 <= nr3 < 10 and 0 <= nc3 < 9) and not visited[nr3][nc3]:
47+
if (nr1, nc1) == (k_r, k_c) or (nr2, nc2) == (k_r, k_c):
48+
# 경로에 왕이 있다면 못 감
49+
continue
50+
51+
visited[nr3][nc3] = True
52+
grid[nr3][nc3] += grid[r][c] + 1
53+
queue.append((nr3, nc3))
54+
55+
return -1 # 도달 불가
56+
57+
58+
print(bfs())

0 commit comments

Comments
 (0)