-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ2178.py
More file actions
55 lines (46 loc) · 1.2 KB
/
BJ2178.py
File metadata and controls
55 lines (46 loc) · 1.2 KB
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
54
55
NM = input().split(' ')
N = int(NM[0])
M = int(NM[1])
arr = []
used = []
tempArr = []
for i in range(N):
user = input()
for j in range(M):
tempArr.append(int(user[j]))
arr.append(tempArr)
tempArr = []
# [x좌표, y좌표, count]
# x좌표가 세로 N, y좌표가 가로 M
queue = [[0, 0, 1]]
count = 1
while queue[0][0] != N - 1 or queue[0][1] != M - 1:
x = queue[0][0]
y = queue[0][1]
count = queue[0][2]
# print(queue)
pop = queue.pop(0)
# print(pop)
count += 1
# 확인 조건: index가 0과 N - 1 또는 M - 1 사이인지
# value가 1인 경우, 넣기! -1이면 넣지 않기!
if x < N - 1:
if arr[x + 1][y] == 1:
queue.append([x + 1, y, count])
arr[x + 1][y] = -1
if x > 0:
if arr[x - 1][y] == 1:
queue.append([x - 1, y, count])
arr[x - 1][y] = -1
if y < M - 1:
if arr[x][y + 1] == 1:
queue.append([x, y + 1, count])
arr[x][y + 1] = -1
if y > 0:
if arr[x][y - 1] == 1:
queue.append([x, y - 1, count])
arr[x][y - 1] = -1
if [N - 1, M - 1, count] in queue:
print(count)
break
# print(count)