Skip to content

Instantly share code, notes, and snippets.

@codyhex
Last active July 27, 2019 21:17
Show Gist options
  • Save codyhex/ab133f46cc9cdb2fe2935107590bc01e to your computer and use it in GitHub Desktop.
Save codyhex/ab133f46cc9cdb2fe2935107590bc01e to your computer and use it in GitHub Desktop.
def get_neighbors_4C(self, pt):
# put self.gird in your class private
x,y = pt[0], pt[1]
# x positive top down, Y to the right
maxX = len(self.grid)
maxY = len(self.grid[0])
# define the neighbor pattern
nc4 = '[[x-1,y], [x+1,y], [x,y-1], [x,y+1]]'
# generate all possible points
pts = eval(nc4)
# then only select the valid ones 这种搞法会浪费内存,但是简单不容易错,适用于大多cases
neighbors = []
for pt in pts:
x,y = pt[0], pt[1]
if 0<=x<maxX and 0<=y<maxY:
neighbors.append([x,y])
return neighbors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment