-
Notifications
You must be signed in to change notification settings - Fork 122
/
Maze.py
54 lines (49 loc) · 1.63 KB
/
Maze.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
class Matrix:
def __init__(self,rows,cols):
self.matrix=[]
for i in range(0,rows):
self.matrix.append([])
self.obstructl=[]
def obstruct(self,x,y):
self.obstructl.append([x,y])
def checksolve(x,y,visited,rows,cols,mat,endx,endy):
if x==endx and y==endy:
return True
if x>=cols or y>=rows:
return False
if x<0 or y<0:
return False
if visited.matrix[y][x] == True:
return False
if [x,y] in mat.obstructl:
return False
visited.matrix[y][x] = True
if checksolve(x+1, y, visited, rows, cols, mat, endx, endy) == True:
return True
if checksolve(x-1, y, visited, rows, cols, mat, endx, endy) == True:
return True
if checksolve(x, y+1, visited, rows, cols, mat, endx, endy) == True:
return True
if checksolve(x, y-1, visited, rows, cols, mat, endx, endy) == True:
return True
return False
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
visited = Matrix(rows,cols)
for i in range(0,rows):
for j in range(0,cols):
visited.matrix[i].append(False)
mat = Matrix(rows,cols)
n = int(input("Enter the number of obstruction points: "))
for i in range(0,n):
x = int(input("X-coordinate: "))
y = int(input("Y-coordinate: "))
mat.obstruct(x,y)
endx = int(input("X-coordinate of end: "))
endy = int(input("Y-coordinate of end: "))
sx = int(input("X-coordinate of start: "))
sy = int(input("Y-coordinate of start: "))
if checksolve(sx,sy,visited,rows,cols,mat,endx,endy) == True:
print("Solvable")
else:
print("Not-Solvable")