Question
Please read the question carefully ! I'm asking for the 3rd time the same question! the python code provided it exceeds the time limit for
Please read the question carefully ! I'm asking for the 3rd time the same question!
the python code provided it exceeds the time limit for most cases! can you help me to revise this code to solve this problem using graph theory in python with the limitations provided?
"it corrects for only 3 cases while exceed the time limit for 7 cases!"
the problem:
Python code:
from copy import deepcopy
## defining function to check if both can meet or not def can_meet(r, c, lake, pos): ## applying bfs using queue in python q = [] vis = [] ## appening the position of Yi and checking it can reach Han q.append([pos[0], pos[1]]) while(len(q)>0): x = q.pop(0) ## returning true if it reaches to Han if(x[0]==pos[2] and x[1]==pos[3]): return True if(x[0]-1>=0 and lake[x[0]-1][x[1]]=='.' and ([x[0]-1, x[1]] not in vis)): q.append([x[0]-1, x[1]]) vis.append([x[0]-1, x[1]]) if(x[0]+1 ## defining function to find time def find_time(r, c, lake): ## finding the position of Yi and Han pos = [] for i in range(r): for j in range(c): if(lake[i][j]=='L'): pos.append(i) pos.append(j) ## counting the number of seconds, after which both Yi cna Han can meet cnt = 0 while(can_meet(r, c, lake, pos)==False): l = deepcopy(lake) for i in range(r): for j in range(c): if(lake[i][j]=='X'): p = 0 if(i-1>=0 and (lake[i-1][j]=='.' or lake[i-1][j]=='L')): p = 1 if(i+1 ## taking input of r and c r, c = input().split() r = int(r) c = int(c) ## taking input of intial position of lake lake = [] l = [] for i in range(r): s = input() lake.append(list(s)) ## calling function to find time print(find_time(r, c, lake)) please revise the code to run with the time and memory limitations with correct output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started