-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.py
69 lines (52 loc) · 1.66 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 28 23:26:19 2018
@author: 45336
"""
#######
#read maze
#######
Maze = []
with open('maze.txt','r') as f:
for line in f.readlines():
Maze.append(line.strip().split(' '))
#######
#find path
#######
def find_path(xstart, ystart, xgoal, ygoal, maze):
'''
if (x,y outside maze) return false
if (x,y is goal) return true
if (x,y not open) return false
mark x,y as part of solution path
if (FIND-PATH(North of x,y) == true) return true
if (FIND-PATH(East of x,y) == true) return true
if (FIND-PATH(South of x,y) == true) return true
if (FIND-PATH(West of x,y) == true) return true
unmark x,y as part of solution path
return false
'''
#maze = [l[:] for l in original_maze]
#maze = original_maze
if xstart < 0 or ystart <0 or xstart > 80 or ystart > 80:
return False
if xstart == xgoal and ystart == ygoal:
return True
if maze[xstart][ystart] != '0':
return False
maze[xstart][ystart] = '+'
if find_path(xstart, ystart-1, xgoal, ygoal, maze) == True:
return True
if find_path(xstart+1, ystart, xgoal, ygoal, maze) == True:
return True
if find_path(xstart, ystart+1, xgoal, ygoal, maze) == True:
return True
if find_path(xstart-1, ystart, xgoal, ygoal, maze) == True:
return True
maze[xstart][ystart] = '-'
return False
#print(find_path(1,34,15,47,Maze))
#print(find_path(1,2,3,39,Maze))
#print(find_path(0,0,3,77,Maze))
#print(find_path(1,75,8,79,Maze))
print(find_path(1,75,39,40,Maze))