Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

Describe the five elements of the listening process.

Answered: 1 week ago