forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_023.py
70 lines (57 loc) · 2.25 KB
/
problem_023.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
from copy import deepcopy
def add_to_cache(coordinate, cache):
new_cache = deepcopy(cache)
new_cache.add("{}-{}".format(coordinate[0], coordinate[1]))
return new_cache
def is_visited(coordinate, cache):
return "{}-{}".format(coordinate[0], coordinate[1]) in cache
def find_path(matrix, rows, cols, start, end, cache):
if start == end:
return 0
cache = add_to_cache(start, cache)
def explore_neighbour(coordinate):
if not is_visited(coordinate, cache) and \
matrix[coordinate[0]][coordinate[1]] != "t":
path_length = find_path(matrix, rows, cols, coordinate, end, cache)
if path_length != None:
path_lengths.append(path_length)
path_lengths = list()
if start[0] != 0:
coordinate = (start[0] - 1, start[1])
explore_neighbour(coordinate)
if start[0] != rows - 1:
coordinate = (start[0] + 1, start[1])
explore_neighbour(coordinate)
if start[1] != 0:
coordinate = (start[0], start[1] - 1)
explore_neighbour(coordinate)
if start[1] != cols - 1:
coordinate = (start[0], start[1] + 1)
explore_neighbour(coordinate)
return min(path_lengths) + 1 if path_lengths else None
matrix = [["f", "f", "f", "f"],
["t", "t", "f", "t"],
["f", "f", "f", "f"],
["f", "f", "f", "f"]]
assert find_path(matrix, len(matrix), len(
matrix[0]), (0, 0), (0, 0), set()) == 0
assert find_path(matrix, len(matrix), len(
matrix[0]), (1, 0), (0, 0), set()) == 1
assert find_path(matrix, len(matrix), len(
matrix[0]), (3, 0), (0, 0), set()) == 7
assert find_path(matrix, len(matrix), len(
matrix[0]), (3, 0), (0, 3), set()) == 6
matrix = [["f", "f", "f", "f"],
["t", "t", "t", "f"],
["f", "f", "f", "f"],
["f", "f", "f", "f"]]
assert find_path(matrix, len(matrix), len(
matrix[0]), (0, 0), (0, 0), set()) == 0
assert find_path(matrix, len(matrix), len(
matrix[0]), (1, 0), (0, 0), set()) == 1
assert find_path(matrix, len(matrix), len(
matrix[0]), (3, 0), (0, 0), set()) == 9
assert find_path(matrix, len(matrix), len(
matrix[0]), (3, 0), (0, 3), set()) == 6
assert find_path(matrix, len(matrix), len(
matrix[0]), (2, 0), (3, 3), set()) == 4