-
Notifications
You must be signed in to change notification settings - Fork 1
/
back_tracking.py
58 lines (41 loc) · 909 Bytes
/
back_tracking.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Back tracking problem
# cook your dish here
def find(res,n,x,y,path):
if x<0 or x>=n:
return False
if y<0 or y>=n:
return False
if x==n-1 and y==n-1:
return True
if res[x][y]==0 or path[x][y]==1:
return False
path[x][y]=1
#Right
if find(res,n,x,y+1,path):
return True
#Left
if find(res,n,x,y-1,path):
return True
#Top
if find(res,n,x-1,y,path):
return True
#Bottom
if find(res,n,x+1,y,path):
return True
return False
def findpath(res,n):
path = [[0]*(n) for i in range(n)]
return find(res,n,0,0,path)
num = int(input())
res = []
for i in range(num):
x = list(map(int,input().split()))
res.append(x)
print(findpath(res,num))
"""
4
1 1 0 0
0 1 0 0
0 1 0 0
0 1 0 1
"""