forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rook.py
47 lines (46 loc) · 1.46 KB
/
rook.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
start = [0, 0]
end = [7, 7]
taken = [[1, 0], [1, 1], [1, 2], [1, 3]]
queue = []
queue.append([start[0], start[1], -1])
visited = []
maze = []
for i in range(8):
maze.append([".", ".", ".", ".", ".", ".", ".", "."])
visited.append([0, 0, 0, 0, 0, 0, 0, 0])
maze[start[0]][start[1]] = "S"
maze[end[0]][end[1]] = "E"
for i in taken:
maze[i[0]][i[1]] = "X"
while len(queue) > 0:
point = queue.pop(0)
if end[0] == point[0] and end[1] == point[1]:
print(point[2] + 1)
break
current = point[2] + 1
if point not in taken and visited[point[0]][point[1]] == 0:
visited[point[0]][point[1]] = current
for i in range(point[0], -1, -1):
if [i, point[1]] in taken:
break
if visited[i][point[1]] == 0:
queue.append([i, point[1], current])
for i in range(point[0], 8):
if [i, point[1]] in taken:
break
if visited[i][point[1]] == 0:
queue.append([i, point[1], current])
for i in range(point[1], -1, -1):
if [point[0], i] in taken:
break
if visited[point[0]][i] == 0:
queue.append([point[0], i, current])
for i in range(point[1], 8):
if [point[0], i] in taken:
break
if visited[point[0]][i] == 0:
queue.append([point[0], i, current])
for i in maze:
for j in i:
print(j, end=" ")
print()