Thank you so much for you help, that got me off on a great start. Just
one more question, I'm starting at one valid house and checking all
possible paths. after that ill have to move to another house and check
the paths on that. How can i make sure i dont get an infinite loop or
recheck a house ive already checked?
使用网格坐标创建一个“House”类:class House(object):
def __init__(self, pos):
self.pos = pos # the coordinates (position) on the grid, a tuple
self.paths = [] # Empty array to hold paths
建造一些房子:
^{pr2}$
现在,穿过每一个房子,计算出它的路径paths = {}
paths[(1,3)] = [(2,3), (4,3) ... ] # possible paths to the point (1,3)
for i in houses:
try:
i.paths = paths[(i.pos)]
except KeyError:
print "I don't know how to get to ", i.pos
单步检查清单可以确保你只检查每间房子一次。现在你可以找出那些无法到达的房子:for i in houses:
if not i.paths:
print "I did not find a way to reach the house at ",i.pos