forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
54 lines (46 loc) · 1.33 KB
/
solution.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
from collections import deque
n = int(input().strip())
gval_line = [0 for _ in range(n)]
gval = [gval_line[:] for _ in range(n)] #trick [:] usage clones gval_line, otherwise one change changes all (like pointer usage)
grid = []
queue = deque([])
for _ in range(n):
grid.append(input().strip())
coords = [int(i) for i in input().strip().split(' ')]
initial = (coords[0], coords[1])
final = (coords[2], coords[3])
queue.append(initial)
while len(queue) != 0:
#print(str(queue))
#print(str(gval))
#print(str(grid))
curr = queue.popleft()
y, x = curr
if curr == final:
print(str(gval[y][x]))
break
cval = gval[y][x] + 1
for i in range(y+1, n):
if grid[i][x] == 'X':
break
elif gval[i][x] == 0:
gval[i][x] = cval
queue.append((i, x))
for i in range(y-1, -1, -1):
if grid[i][x] == 'X':
break
elif gval[i][x] == 0:
gval[i][x] = cval
queue.append((i, x))
for i in range(x+1, n):
if grid[y][i] == 'X':
break
elif gval[y][i] == 0:
gval[y][i] = cval
queue.append((y, i))
for i in range(x-1, -1, -1):
if grid[y][i] == 'X':
break
elif gval[y][i] == 0:
gval[y][i] = cval
queue.append((y, i))