'Matrix Solver If statement
So I was programming this Matrix Solver and the only problem i had with it, how to find out what cells are next to me and if they are clear. So I found this long if and or statement and wanted to know, what it execatly means since the rest of the code is very simple. Or if there is an easier way to check the cells which are near you.
grid = [[0, "S", 0, 0, 0, 1],
[1, 1, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0],
[0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 2, 0, 0, 0]]
def move(x, j):
if grid[x][j] == 2:
print('found at %d,%d' % (x, j))
return True
elif grid[x][j] == 1:
print ('wall at %d,%d' % (x, j))
return False
elif grid[x][j] == 3:
print ('visited at %d,%d' % (x, j))
return False
print( 'visiting %d,%d' % (x, j))
grid[x][j] = 3
#Look to the cells next to your current cell
if ((x < len(grid)-1 and move(x+1, j))
or (j > 0 and move(x, j-1))
or (x > 0 and move(x-1, j))
or (j < len(grid)-1 and move(x, j+1))):
return True
return False
def find(m):
for x in range(len(m)):
for j in range(len(m[0])):
if m[x][j] == "S":
return x, j
x,j = find(grid)
move(x, j)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
