Link : judge.koreatech.ac.kr/problem.php?id=1046
Python
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
from sys import stdin
import queue
data = []
def able_move(x, y):
if x < 0 or y < 0:
return False
if x >= map_x or y >= map_y:
return False
if map_data[y][x] == '-' or map_data[y][x] == 'E' or map_data[y][x] == 'S':
return True
else:
return False
def BFS(p_x, p_y):
que = queue.Queue()
que.put((p_x, p_y, 0))
while que.qsize():
p_x, p_y, p_d = que.get()
if able_move(p_x, p_y):
if map_data[p_y][p_x] == 'E':
return p_d
map_data[p_y][p_x] = '@'
if able_move(p_x + 1, p_y):
que.put((p_x + 1, p_y, p_d + 1)) # right
if able_move(p_x - 1, p_y):
que.put((p_x - 1, p_y, p_d + 1)) # left
if able_move(p_x, p_y + 1):
que.put((p_x, p_y + 1, p_d + 1)) # down
if able_move(p_x, p_y - 1):
que.put((p_x, p_y - 1, p_d + 1)) # up
return -1
testcase = int(stdin.readline().strip())
while testcase:
map_y, map_x = list(map(int, stdin.readline().strip().split(' ')))
map_data = [[s for s in stdin.readline().strip()] for i in range(map_y)]
start = (0, 0)
for t_y, i in enumerate(map_data):
for t_x, j in enumerate(i):
if j == 'S':
start = (t_x, t_y)
print(BFS(start[0], start[1]))
testcase -= 1
|
cs |
FeedBack
cjw.git@gmail.com
'알고리즘 > 소스코드' 카테고리의 다른 글
PROGRAMMERS 60059: 자물쇠와 열쇠 (0) | 2021.01.25 |
---|---|
KOREATECH 1074: 유일한 수 두개 (0) | 2021.01.23 |
KOREATECH 1043: 위성 사진 (0) | 2021.01.21 |
PROGRAMMERS 43164: 여행경로 (0) | 2021.01.14 |
PROGRAMMERS 17687: [3차] n진수 게임 (0) | 2021.01.14 |
댓글