-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex05e1_shortest.py
72 lines (63 loc) · 1.29 KB
/
ex05e1_shortest.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#14:30
import sys
input = sys.stdin.readline
print = lambda s:sys.stdout.write(str(s)+"\n")
def BFS(table,r,c):
V = [ [0]*c for i in range(r) ]
M = [ [] ]
for i in range(r):
for j in range(c):
if table[i][j] == ".":
V[i][j] = 0
else:
V[i][j] = 2
q = [(0,0)]
d = 0
f = False
while len(q)!=0:
newQ = []
for ix,iy in q:
if ix==r-1 and iy==c-1:
f=True
break
V[ix][iy] = 1
if ix-1>=0 and V[ix-1][iy]==0:
newQ.append( (ix-1,iy) )
if iy-1>=0 and V[ix][iy-1]==0:
newQ.append( (ix,iy-1) )
if ix+1<r and V[ix+1][iy]==0:
newQ.append( (ix+1,iy) )
if iy+1<c and V[ix][iy+1]==0:
newQ.append( (ix,iy+1) )
if f==True:
break
if f==True:
break
q = newQ
d+=1
if f==True:
print(d)
else:
print(-1)
r,c = [int(i) for i in input().split()]
table = [ [] for i in range(r) ]
for i in range(r):
l = input()
for j in range(c):
table[i].append(l[j])
#print(table)
BFS(table,r,c)
'''
5 5
.....
...##
...#.
..##.
.###.
5 5
.#...
.#...
.#...
.#.#.
...#.
'''