Is it possible to combine conditions for the value of the cell and for it's position being in the list into one?

if i > 0 and carver_map[i-1][j] == 'empty': carvable.add([j, i])
if j > 0 and carver_map[i][j-1] == 'empty': carvable.add([j, i])
if j < m-1 and carver_map[i][j+1] == 'empty': carvable.add([j, i])
if i < n-1 and carver_map[i+1][j] == 'empty': carvable.add([j, i])
reddit.com
u/SmurfCat2281337 — 12 days ago

Trying to mage pathfinding, it appears the visited cells list is shared between the recursion parts. Is it possible to make it so every step has access only to its own version of it that has only the cells that exact path visited?

def pathfind(X_start, Y_start, X_end, y_end, current_path = '', cost = 0, last='', visited = []):
    cost += field[Y_start][X_start]
    if [X_start, Y_start] not in visited:
        visited.append([X_start, Y_start])
        print(visited)
        if X_start > 0 and last != 'd':
            subpaths[current_path + 'a'] = cost + field[Y_start][X_start-1]
            pathfind(X_start-1, Y_start, X_end, y_end, current_path + 'a', cost, 'a', visited)
        if Y_start > 0 and last != 's':
            subpaths[current_path + 'w'] = cost + field[Y_start-1][X_start]
            pathfind(X_start, Y_start-1, X_end, y_end, current_path + 'w', cost, 'w', visited)
        if X_start < 4 and last != 'a':
            subpaths[current_path + 'd'] = cost + field[Y_start][X_start+1]
            pathfind(X_start+1, Y_start, X_end, y_end, current_path + 'd', cost, 'd', visited)
        if Y_start < 4 and last != 'w':
            subpaths[current_path + 's'] = cost + field[Y_start+1][X_start]
            pathfind(X_start, Y_start+1, X_end, y_end, current_path + 's', cost, 's', visited)def pathfind(X_start, Y_start, X_end, y_end, current_path = '', cost = 0, last='', visited = []):
    cost += field[Y_start][X_start]
    if [X_start, Y_start] not in visited:
        visited.append([X_start, Y_start])
        print(visited)
        if X_start > 0 and last != 'd':
            subpaths[current_path + 'a'] = cost + field[Y_start][X_start-1]
            pathfind(X_start-1, Y_start, X_end, y_end, current_path + 'a', cost, 'a', visited)
        if Y_start > 0 and last != 's':
            subpaths[current_path + 'w'] = cost + field[Y_start-1][X_start]
            pathfind(X_start, Y_start-1, X_end, y_end, current_path + 'w', cost, 'w', visited)
        if X_start < 4 and last != 'a':
            subpaths[current_path + 'd'] = cost + field[Y_start][X_start+1]
            pathfind(X_start+1, Y_start, X_end, y_end, current_path + 'd', cost, 'd', visited)
        if Y_start < 4 and last != 'w':
            subpaths[current_path + 's'] = cost + field[Y_start+1][X_start]
            pathfind(X_start, Y_start+1, X_end, y_end, current_path + 's', cost, 's', visited)
reddit.com
u/SmurfCat2281337 — 3 months ago