Skip to content

Instantly share code, notes, and snippets.

@lewtds
Created September 27, 2017 17:49
Show Gist options
  • Save lewtds/6086ca10aaf8ad35b9f11b02eca9fad4 to your computer and use it in GitHub Desktop.
Save lewtds/6086ca10aaf8ad35b9f11b02eca9fad4 to your computer and use it in GitHub Desktop.
def neighbors(row, col, rows, cols):
n = []
for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if 0 <= col + i and col + i < cols and 0 <= row + j and row + j < rows:
n.append((row + j, col + i))
return n
def get_number_of_reachable_fields(grid, rows, columns, start_row, start_column):
checked = set()
to_search = set()
reachable = set()
to_search.add((start_row, start_column))
# do a breadth first search
while len(to_search) > 0:
current = to_search.pop()
checked.add(current)
row, col = current
# print current
print row, col, neighbors(row, col, rows, columns)
for nrow, ncol in neighbors(row, col, rows, columns):
if grid[nrow][ncol] and (nrow, ncol) not in checked:
reachable.add((nrow, ncol))
to_search.add((nrow, ncol))
print reachable
return len([pair for pair in reachable if pair[0] == rows - 1])
@max-prokopenko
Copy link

Breadth First Search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment