Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def d(s):
- raw_input("----DEBUG----{0}".format(s))
- class City:
- # parses the intersections from a string s
- def __init__(self, s):
- nums = s.split()
- self.juncs = set(map(int,nums))
- self.roads = dict()
- for i in range(0, len(nums), 2):
- if int(nums[i]) in self.roads:
- self.roads[int(nums[i])].append(int(nums[i+1]))
- else:
- self.roads[int(nums[i])] = [int(nums[i+1])]
- def paths(self, a, b, current_path=[]):
- # IF
- # the start point and end point are the same at the beginning,
- # junction 'a' does not exist
- # junction 'b' does not exist
- # there are not outgoing roads from 'a'
- # return nothing
- if (current_path == [] and a == b) or a not in self.juncs or b not in self.juncs or not self.roads.has_key(a):
- return []
- # add the starting junction to the path
- current_path = current_path + [a]
- # If we've reached the endpoint,
- if a == b:
- return [current_path]
- # prepare the return
- thispath = []
- # for every junction reachable from a
- for junction in self.roads[a]:
- # if we haven't already visited this junction,
- if junction not in current_path:
- # send out a probe finding all paths from this junction to the ending point
- probe = self.paths(junction, b, current_path)
- # add all the paths found
- for potential_path in probe:
- thispath.append(potential_path)
- # return the paths found from junction a to junction b
- return thispath
- # takes an input string and returns a list of Cities
- def parse(s):
- print "p"
- t = City("0 1 0 2 0 4 2 4 2 3 3 1 4 3")
- print t.paths(0,3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement