-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_DS.py
52 lines (41 loc) · 1.42 KB
/
maze_DS.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
def showmaze(maze, N, depth):
print('-------------------------------',end='\n')
print('Step No:' + str(depth),end='\n')
for i in range(N):
print(maze[i], end='\n')
print('-------------------------------',end='\n')
def DS(x, y, depth):
# ゴールについたとき
if maze[x][y] == 1:
print(depth)
exit()
showmaze(maze, N, depth)
# ゴールじゃないとき、探索済みとする
maze[x][y] = 2
#現在位置の上下左右を探索する。いけるところまでぐんぐん突き進んでいく。
if maze[x - 1][y] < 2:
DS(x - 1, y, depth + 1)
if maze[x + 1][y] < 2:
DS(x + 1, y, depth + 1)
if maze[x][y - 1] < 2:
DS(x, y - 1, depth + 1)
if maze[x][y + 1] < 2:
DS(x, y + 1, depth + 1)
maze[x][y] = 0
if __name__ == '__main__':
maze = [
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 9],
[9, 0, 9, 0, 0, 0, 9, 9, 0, 9, 9, 9],
[9, 0, 9, 9, 0, 9, 0, 0, 0, 9, 0, 9],
[9, 0, 0, 0, 9, 0, 0, 9, 9, 0, 9, 9],
[9, 9, 9, 0, 0, 9, 0, 9, 0, 0, 0, 9],
[9, 0, 0, 0, 9, 0, 9, 0, 0, 9, 1, 9],
[9, 0, 9, 0, 0, 0, 0, 9, 0, 0, 9, 9],
[9, 0, 0, 9, 0, 9, 0, 0, 9, 0, 0, 9],
[9, 0, 9, 0, 9, 0, 9, 0, 0, 9, 0, 9],
[9, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
]
N = 12
DS(1, 1, 0)