Path in a Rectangle with Circles | Python3 program to find out path in a rectangle containing circles . ; Function to find out if there is any possible path or not . ; Take an array of m * n size and initialize each element to 0. ; Now using Pythagorean theorem find if a cell touches or within any circle or not . ; If the starting cell comes within any circle return false . ; Now use BFS to find if there is any possible path or not . Initialize the queue which holds the discovered cells whose neighbors are not discovered yet . ; Discover cells until queue is not empty ; Discover the eight adjacent nodes . check top - left cell ; check top cell ; check top - right cell ; check left cell ; check right cell ; check bottom - left cell ; check bottom cell ; check bottom - right cell ; Now if the end cell ( i . e . bottom right cell ) is 1 ( reachable ) then we will send true . ; Driver Code ; Test case 1 ; Function call ; Test case 2 ; Function call
import math NEW_LINE import queue NEW_LINE def isPossible ( m , n , k , r , X , Y ) : NEW_LINE INDENT rect = [ [ 0 ] * n for i in range ( m ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT for p in range ( k ) : NEW_LINE INDENT if ( math . sqrt ( ( pow ( ( X [ p ] - 1 - i ) , 2 ) + pow ( ( Y [ p ] - 1 - j ) , 2 ) ) ) <= r ) : NEW_LINE INDENT rect [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( rect [ 0 ] [ 0 ] == - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT qu = queue . Queue ( ) NEW_LINE rect [ 0 ] [ 0 ] = 1 NEW_LINE qu . put ( [ 0 , 0 ] ) NEW_LINE while ( not qu . empty ( ) ) : NEW_LINE INDENT arr = qu . get ( ) NEW_LINE elex = arr [ 0 ] NEW_LINE eley = arr [ 1 ] NEW_LINE if ( ( elex > 0 ) and ( eley > 0 ) and ( rect [ elex - 1 ] [ eley - 1 ] == 0 ) ) : NEW_LINE INDENT rect [ elex - 1 ] [ eley - 1 ] = 1 NEW_LINE v = [ elex - 1 , eley - 1 ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT if ( ( elex > 0 ) and ( rect [ elex - 1 ] [ eley ] == 0 ) ) : NEW_LINE INDENT rect [ elex - 1 ] [ eley ] = 1 NEW_LINE v = [ elex - 1 , eley ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT if ( ( elex > 0 ) and ( eley < n - 1 ) and ( rect [ elex - 1 ] [ eley + 1 ] == 0 ) ) : NEW_LINE INDENT rect [ elex - 1 ] [ eley + 1 ] = 1 NEW_LINE v = [ elex - 1 , eley + 1 ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT if ( ( eley > 0 ) and ( rect [ elex ] [ eley - 1 ] == 0 ) ) : NEW_LINE INDENT rect [ elex ] [ eley - 1 ] = 1 NEW_LINE v = [ elex , eley - 1 ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT if ( ( eley < n - 1 ) and ( rect [ elex ] [ eley + 1 ] == 0 ) ) : NEW_LINE INDENT rect [ elex ] [ eley + 1 ] = 1 NEW_LINE v = [ elex , eley + 1 ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT if ( ( elex < m - 1 ) and ( eley > 0 ) and ( rect [ elex + 1 ] [ eley - 1 ] == 0 ) ) : NEW_LINE INDENT rect [ elex + 1 ] [ eley - 1 ] = 1 NEW_LINE v = [ elex + 1 , eley - 1 ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT if ( ( elex < m - 1 ) and ( rect [ elex + 1 ] [ eley ] == 0 ) ) : NEW_LINE INDENT rect [ elex + 1 ] [ eley ] = 1 NEW_LINE v = [ elex + 1 , eley ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT if ( ( elex < m - 1 ) and ( eley < n - 1 ) and ( rect [ elex + 1 ] [ eley + 1 ] == 0 ) ) : NEW_LINE INDENT rect [ elex + 1 ] [ eley + 1 ] = 1 NEW_LINE v = [ elex + 1 , eley + 1 ] NEW_LINE qu . put ( v ) NEW_LINE DEDENT DEDENT return ( rect [ m - 1 ] [ n - 1 ] == 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m1 = 5 NEW_LINE n1 = 5 NEW_LINE k1 = 2 NEW_LINE r1 = 1 NEW_LINE X1 = [ 1 , 3 ] NEW_LINE Y1 = [ 3 , 3 ] NEW_LINE if ( isPossible ( m1 , n1 , k1 , r1 , X1 , Y1 ) ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT m2 = 5 NEW_LINE n2 = 5 NEW_LINE k2 = 2 NEW_LINE r2 = 1 NEW_LINE X2 = [ 1 , 1 ] NEW_LINE Y2 = [ 2 , 3 ] NEW_LINE if ( isPossible ( m2 , n2 , k2 , r2 , X2 , Y2 ) ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT