Question: def find _ paths ( matrix , n , oxygen _ capacity ) : def dfs ( x , y , path, oxygen ) :

def find_paths(matrix, n, oxygen_capacity):
def dfs(x, y, path, oxygen):
nonlocal found_paths
if x <0 or x >= n or y <0 or y >= n or matrix[x][y]=='O' or visited[x][y]:
return
oxygen -=2* int(matrix[x][y])
if oxygen <0:
return
visited[x][y]= True
path.append(directions[(x, y)])
if (x, y)==(0, n -1) or (x, y)==(n -1, n -1):
found_paths.append((''.join(path), oxygen))
for dx, dy in directions.values():
dfs(x + dx, y + dy, path, oxygen)
path.pop()
visited[x][y]= False
directions ={
(0,1): 'R',
(1,0): 'D',
(0,-1): 'L',
(-1,0): 'U',
}
visited =[[False]* n for _ in range(n)]
found_paths =[]
dfs(0,0,[], oxygen_capacity)
return found_paths
# Input
n = int(input("Enter the value of n: "))
matrix =[input().strip() for _ in range(n)]
oxygen_capacity = int(input("Enter the oxygen capacity: "))
# Finding paths
result = find_paths(matrix, n, oxygen_capacity)
# Output
if not result:
print("No path available to reach the destination")
elif not any(oxygen >=0 for _, oxygen in result):
print("No feasible path")
else:
print("The available paths are")
for path, _ in result:
print(path)
print("The feasible paths with remaining oxygen levels are")
for path, oxygen in result:
if oxygen >=0:
print(f"{path}{oxygen}")
is this correct?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!