- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRobot.py
More file actions
Latest commit
69 lines (57 loc) · 1.45 KB
/
Copy pathRobot.py
File metadata and controls
69 lines (57 loc) · 1.45 KB
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
'''
imagine a robot sitting on the upper left hand corner of an N*N grid,
the robot can only move in two directions: right and down
How many possible paths are there for the robot?
FLLOW UP
imagine certain squares are 'off limits',such that the robot can not step on
them
Design an algorithm to get all possible paths for the robot
'''
N=3
classPoint(object):
def__init__(self,x,y):
self.x=x
self.y=y
def__repr__(self):
return'('+str(self.x) +','+str(self.y) +')'
defno_moves(point):
'''
cannnot move reach the border
'''
returnpoint.x==Norpoint.y==N
defnext_moves(point):
'''
given the current loctation,
get the possible next moves,
whether it can move right and down
return the list of next moves
'''
nexts= []
ifpoint.x+1<N:
nexts.append(Point(point.x+1, point.y))
ifpoint.y+1<N:
nexts.append(Point(point.x, point.y+1))
returnnexts
defrobot_moves(start):
'''
given the start position of the robot,
find all possible moves
'''
queue= []
queue.append([start])
num_paths_found=0
whilequeue:
path=queue.pop(0)
printpath
ifno_moves(path[-1]):
num_paths_found+=1
else:
formoveinnext_moves(path[-1]):
# print str(move)
new_path=path[:]
new_path.append(move)
queue.append(new_path)
returnnum_paths_found
# print has_more_move(1,1)
start=Point(0,0)
printrobot_moves(start)