import json def find_next_coords(matrix, coords, travelled): [x, y] = coords left, right, down, up = [x, y - 1], [x, y + 1], [x + 1, y], [x - 1, y] next_coords = list() for coords in [left, right, down, up]: [coords_x, coords_y] = coords if coords_x > 3 or coords_x < 0 or coords_y > 3 or coords_y < 0: continue already_travelled = False for travelled_coords in travelled: if travelled_coords[0] == coords_x and travelled_coords[1] == coords_y: already_travelled = True break if matrix[coords_x] and matrix[coords_x][coords_y] and not already_travelled: next_coords.append(coords) return next_coords def can_travel(matrix, start, end, travelled): [start_x, start_y] = start [end_x, end_y] = end travelled.append(start) if start_x == end_x and start_y == end_y: return True next_coords_to_go = find_next_coords(matrix, start, travelled) for coords in next_coords_to_go: if can_travel(matrix, coords, end, travelled): return True return False def check_traversilibility(matrix, start, end): output = "no" if matrix[start[0]][start[1]] == 0: print(output) exit() if can_travel(matrix, start, end, list()): output = "yes" print(output) matrix = json.loads("[ [0, 1, 1, 1], [1, 1, 0, 1], [1, 0, 0, 1], [1, 1, 0, 0] ]") start = json.loads("[ 0, 3 ]") end = json.loads("[ 3, 1 ]") check_traversilibility(matrix, start, end)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question