# 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 . ; 

import math
import queue


def isPossible(m, n, k, r, X, Y):
    rect = [[0] * n for i in range(m)]
    for i in range(m):
        for j in range(n):
            for p in range(k):
                if (math .sqrt(
                        (pow((X[p] - 1 - i), 2) + pow((Y[p] - 1 - j), 2))) <= r):
                    rect[i][j] = -1
    if (rect[0][0] == -1):
        return False
    qu = queue .Queue()
    rect[0][0] = 1
    qu .put([0, 0])
    while (not qu .empty()):
        arr = qu .get()
        elex = arr[0]
        eley = arr[1]
        if ((elex > 0) and (eley > 0) and (rect[elex - 1][eley - 1] == 0)):
            rect[elex - 1][eley - 1] = 1
            v = [elex - 1, eley - 1]
            qu .put(v)
        if ((elex > 0) and (rect[elex - 1][eley] == 0)):
            rect[elex - 1][eley] = 1
            v = [elex - 1, eley]
            qu .put(v)
        if ((elex > 0) and (eley < n - 1) and (rect[elex - 1][eley + 1] == 0)):
            rect[elex - 1][eley + 1] = 1
            v = [elex - 1, eley + 1]
            qu .put(v)
        if ((eley > 0) and (rect[elex][eley - 1] == 0)):
            rect[elex][eley - 1] = 1
            v = [elex, eley - 1]
            qu .put(v)
        if ((eley < n - 1) and (rect[elex][eley + 1] == 0)):
            rect[elex][eley + 1] = 1
            v = [elex, eley + 1]
            qu .put(v)
        if ((elex < m - 1) and (eley > 0) and (rect[elex + 1][eley - 1] == 0)):
            rect[elex + 1][eley - 1] = 1
            v = [elex + 1, eley - 1]
            qu .put(v)
        if ((elex < m - 1) and (rect[elex + 1][eley] == 0)):
            rect[elex + 1][eley] = 1
            v = [elex + 1, eley]
            qu .put(v)
        if ((elex < m - 1) and (eley < n - 1)
                and (rect[elex + 1][eley + 1] == 0)):
            rect[elex + 1][eley + 1] = 1
            v = [elex + 1, eley + 1]
            qu .put(v)
    return (rect[m - 1][n - 1] == 1)


if __name__ == '__main__':
    m1 = 5
    n1 = 5
    k1 = 2
    r1 = 1
    X1 = [1, 3]
    Y1 = [3, 3]
    if (isPossible(m1, n1, k1, r1, X1, Y1)):
        print("Possible")
    else:
        print("Not Possible")
    m2 = 5
    n2 = 5
    k2 = 2
    r2 = 1
    X2 = [1, 1]
    Y2 = [2, 3]
    if (isPossible(m2, n2, k2, r2, X2, Y2)):
        print("Possible")
    else:
        print("Not Possible")

# Expected output:
Possible
Not Possible