这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions 12_Route_Planner/solution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
def route_exists(from_row, from_column, to_row, to_column, map_matrix):
pass
rows = len(map_matrix)
cols = len(map_matrix[0])

stack = [(from_row, from_column)]
visited = set()

while stack:
current = stack.pop()
if current == (to_row, to_column):
return True
visited.add(current)
row, col = current
neighbors = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for neighbor in neighbors:
n_row, n_col = neighbor
if 0 <= n_row < rows and 0 <= n_col < cols and map_matrix[n_row][n_col] and neighbor not in visited:
stack.append(neighbor)

return False

if __name__ == '__main__':
map_matrix = [
[True, False, False],
[True, True, False],
[False, True, True]
];
]

print(route_exists(0, 0, 2, 2, map_matrix))