-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Maze Problem.py
39 lines (33 loc) · 876 Bytes
/
Maze Problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def issafe(p,q,arr):
#print(type(p),type(n))
if p<n and p>=0 and q<n and q>=0 and arr[p][q]==1:
#print("Sdsdsd")
return True
return False
def solve(arr,i,j,sol):
#print(n)
if i==n-1 and j==n-1:
sol[i][j]=1
return True
#print(i,j)
if issafe(i,j,arr)==True:
sol[i][j]=1
#print(sol)
if solve(arr,i,j+1,sol)==True:
return True
if solve(arr,i+1,j,sol)==True:
return True
sol[i][j]=0
return False
t = int(input())
for _ in range(t):
n = int(input())
arr =[]
sol = [[ 0 for i in range(n) ] for i in range(n)]
for i in range(n):
x = list(map(int,input().split()))
arr.append(x)
if solve(arr,0,0,sol) ==True:
print("POSSIBLE")
else:
print("NOT POSSIBLE")