Skip to content

Instantly share code, notes, and snippets.

@DexGroves
Created February 10, 2017 15:04
Show Gist options
  • Save DexGroves/4e04e922be5078ba326f213d75f7cc74 to your computer and use it in GitHub Desktop.
Save DexGroves/4e04e922be5078ba326f213d75f7cc74 to your computer and use it in GitHub Desktop.
import numpy as np
from collections import deque
def dist_to(game_map, sources):
dist = np.empty_like(game_map.owners)
dist.fill(-1)
for sx, sy in sources:
dist[sx, sy] = 0
ds = deque(sources)
while len(ds) > 0:
cx, cy = ds.popleft()
c_dist = dist[cx, cy]
for nx, ny in game_map.nbrs[cx, cy]:
if dist[nx, ny] == -1 or dist[nx, ny] > (c_dist + 1):
dist[nx, ny] = c_dist + 1
ds.append((nx, ny))
return dist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment