Skip to content

Instantly share code, notes, and snippets.

Last active July 27, 2019 21:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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:
return neighbors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment