Created
October 11, 2019 20:33
-
-
Save ahmed4end/a430f330c9876f93de75e0b2016b0097 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#(0: clear step), (5 or any number other than zero: obstacles) | |
state = [[0,0,0,0,0], | |
[0,5,5,5,0], | |
[0,5,5,5,0], | |
[0,5,5,5,0], | |
[0,0,0,0,0]] | |
def access(state, x, y): | |
try:return state[x][y] | |
except:pass | |
neighbours = lambda x, state, player:list(filter(lambda x:x is not False,map(lambda x:x if 0<=x[0]<=len(state[0])-1 and x[1]>=0 and access(state, x[0], x[1]) == player else False, ((x[0]-1, x[1]),(x[0]+1, x[1]),(x[0],x[1]+1),(x[0],x[1]-1))))) | |
def finder(state, start, end, trac=[], paths=[], depth=0): | |
if start == end:return True | |
for row, col in set(neighbours(start, state, 0))-{start}: | |
depth += 1 | |
state[row][col] = 5 | |
trac.append(start) | |
if finder(state, start=(row, col), end=end, depth=depth) == True:paths.append(trac[::]+[(3,3)]) | |
state[row][col] = 0 | |
trac.remove(start) | |
depth -= 1 | |
if depth == 0: | |
minimum = len(min(paths, key=len)) | |
paths = [i for i in paths if len(i)==minimum] | |
return paths | |
if __name__ == "__main__": | |
for i in finder(state, (0,0), (4,4)): | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment