import sys, math def bfs_paths(graph, start, goal): queue = [(start, [start])] while queue: (vertex, path) = queue.pop(0) for next in graph[vertex] - set(path): if next in goal: yield path + [next] else: queue.append((next, path + [next])) def shortest_path(graph, start, goal): try: return next(bfs_paths(graph, start, goal)) except StopIteration: return None def severRoute(route, graph): graph[rout[0]].remove # Auto-generated code below aims at helping you parse # the standard input according to the problem statement. # N: the total number of nodes in the level, including the gateways # L: the number of links # E: the number of exit gateways N, L, E = [int(i) for i in raw_input().split()] graph = {} for i in xrange(L): # N1: N1 and N2 defines a link between these nodes n1,n2 = [int(j) for j in raw_input().split()] if n1 in graph: graph[n1].add(n2) else: graph[n1] = set([n2]) if n2 in graph: graph[n2].add(n1) else: graph[n2] = set([n1]) gateways = [int(raw_input()) for k in xrange(E)] # the index of a gateway node print >>sys.stderr, "Gateways: "+ str(gateways) # game loop while 1: agent = int(raw_input()) # The index of the node on which the Skynet agent is positioned this turn likelyRoute = shortest_path(graph,agent,gateways) print >>sys.stderr,"Likely agent route: "+ str(likelyRoute) # Write an action using print # To debug: print >> sys.stderr, "Debug messages..." severedLink = severRoute(likelyRoute,graph) print >>sys.stderr,"severed link: "+ str(severedLink) print severedLink[0]+" "+severedLink[1] # Example: 0 1 are the indices of the nodes you wish to sever the link between
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question