-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdfs_sweeper.py
64 lines (52 loc) · 2.15 KB
/
dfs_sweeper.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
class DFSSweeper(object):
def __init__(self, robot):
self.observed_map = {}
self.robot = robot
self.loggable = True
def sweep(self):
self.move({'x': 0, 'y': 0}, 0)
def move(self, cur, dir):
self.observed_map[str(cur['x'])+'_'+str(cur['y'])] = 1
straight = self.next_straight(cur, dir)
if not self.visited(straight) and self.robot.move():
self.move(straight, dir)
turn_taken = 0
right = self.next_right(cur, dir)
if not self.visited(right):
self.robot.turn_right()
turn_taken += 1
if self.robot.move():
self.move(right, (dir + 1) % 4)
down = self.next_down(cur, dir)
if not self.visited(down):
for _ in range(2 - turn_taken):
self.robot.turn_right()
turn_taken += 1
if self.robot.move():
self.move(down, (dir + 2) % 4)
left = self.next_left(cur, dir)
if not self.visited(left):
for _ in range(3 - turn_taken):
self.robot.turn_right()
turn_taken += 1
if self.robot.move():
self.move(left, (dir + 3) % 4)
left_turns = turn_taken - 2
if left_turns < 0:
for _ in range(abs(left_turns)):
self.robot.turn_right()
else:
for _ in range(left_turns):
self.robot.turn_left()
self.robot.move()
self.robot.turn_left().turn_left()
def next_straight(self, cur, dir):
return {'x': cur['x'] - ((dir + 1) % 2) * (dir - 1), 'y': cur['y'] - (dir % 2) * (dir - 2)}
def next_right(self, cur, dir):
return {'x': cur['x'] + (dir % 2) * (dir - 2), 'y': cur['y'] - ((dir + 1) % 2) * (dir - 1)}
def next_left(self, cur, dir):
return {'x': cur['x'] - (dir % 2) * (dir - 2), 'y': cur['y'] + ((dir + 1) % 2) * (dir - 1)}
def next_down(self, cur, dir):
return {'x': cur['x'] + ((dir + 1) % 2) * (dir - 1), 'y': cur['y'] + (dir % 2) * (dir - 2)}
def visited(self, node):
return (str(node['x'])+'_'+str(node['y'])) in self.observed_map