Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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: image text in transcribed

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=0 and lake[x[0]][x[1]-1]=='.' and ([x[0], x[1]-1] not in vis)): q.append([x[0], x[1]-1]) vis.append([x[0], x[1]-1]) if(x[1]+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=0 and (lake[i][j-1]=='.' or lake[i][j-1]=='L')): p = 1 if(j+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.

time limit per test: 2 secondso memory limit per test: 1024 megabytes input: standard input output: standard output Yi and Han are two swans living on a lake. They are good friends and often play together. One day they woke up and found out that because of the snow last night, they were separate in two different areas by ice. The lake can be viewed as a rc grid. We will use " X " to represent a cell of ice. and "." a cell as water. At the beginning, Yi and Han are both at a cell of water. Every day, some ice cells " X " will melt. An ice cell " X " melts if and only if in the previcus day, at least one of its adjacent grids is water. Here "adjacent" means on the top, bottom, left and right, at most 4 of them. An example of the ice melting process is shown above. Yi and Han can both swim to any adjacent water cell from their current position, but they cannot swim to any ice cells. They both swim very fast so that they can arrive at any cell on the lake instantly as long as two cells are connected with water cells. Given the initial position of Yi and Han, and the current status of the lake, decide that at which day Yi and Han can meet again. Input The first line contains two integers r and c,1r,c1500. The following r lines each contains c characters, which describes the initial status of the lake. " "means a water cell. " X " means an ice cell, "L" means a swan. Output The output only contains one integer t, which means that after t days, Y and Han can meet

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

Students also viewed these Databases questions

Question

=+2. Does your message use dishonest or misleading language?

Answered: 1 week ago

Question

Describe the following terms.

Answered: 1 week ago

Question

What is meant by Career Planning and development ?

Answered: 1 week ago

Question

What are Fringe Benefits ? List out some.

Answered: 1 week ago

Question

1. Discuss the four components of language.

Answered: 1 week ago

Question

f. What stereotypes were reinforced in the commercials?

Answered: 1 week ago