Skip to content

Commit 41e138e

Browse files
authored
Create the-maze-iv.py
1 parent 93a76f5 commit 41e138e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

the-maze-iv.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
"""
3+
@param maps:
4+
@return:
5+
"""
6+
def the_maze_i_v(self, maps):
7+
#
8+
ret = 0
9+
md = 0
10+
dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]]
11+
paths = [set(), set()]
12+
curr = 0
13+
rows, cols = len(maps), len(maps[0])
14+
for i in range(rows):
15+
for j in range(cols):
16+
if maps[i][j] == 'S':
17+
paths[curr].add((i, j))
18+
while paths[curr]:
19+
if ret == 32:
20+
k = 0
21+
for i in range(rows):
22+
for j in range(cols):
23+
if maps[i][j] == '.':
24+
k += 1
25+
for p in paths[curr]:
26+
if maps[p[0]][p[1]] == 'T':
27+
return ret
28+
for d in dirs:
29+
r = p[0] + d[0]
30+
c = p[1] + d[1]
31+
if (r >= 0) and (r < rows) and (c >= 0) and (c < cols) and ((maps[r][c] == '.') or (maps[r][c] == 'T')):
32+
paths[1 - curr].add((r, c))
33+
maps[p[0]][p[1]] = '#'
34+
paths[curr].clear()
35+
curr = 1 - curr
36+
ret += 1
37+
return -1
38+
39+
# medium: https://www.lintcode.com/problem/1685/

0 commit comments

Comments
 (0)