|
有向图,用dict实现,如下以杨州生成的部分有向图
- {'24': {u'23': u'west'}, '25': {u'23': u'south'},
- '26': {u'1': u'west', u'31': u'east', u'27': u'north', u'30': u'south'},
- '27': {u'26': u'south', u'28': u'north', u'29': u'west'},
- '20': {u'19': u'east'}, '21': {u'19': u'west'},
- '22': {u'19': u'down'},
- '23': {u'24': u'east', u'25': u'north', u'12': u'south'},
- '28': {u'27': u'south'}, '29': {u'27': u'east'},
- '4': {u'1': u'south', u'10': u'southeast', u'12': u'north', u'5': u'west', u'7': u'east'},
- '8': {u'9': u'enter', u'7': u'down'},
- '79': {u'75': u'west'}, '78': {u'75': u'east'}}
复制代码 求两点的最短路径
- def find_link(graph, start, end, path = []):
- start = str(start)
- end = str(end)
- path = path + [start]
- if start == end: return path
- if not graph.has_key(start): return None
- shortest = None
- for node, value in graph[start].items():
- if node not in path:
- newpath = find_link(graph, node, end, path)
- if newpath:
- if not shortest or len(newpath) < len(shortest):
- shortest = newpath
- return shortest
复制代码 graph为如上所示的当前地图所有房间生成的有向图
start为起点,end为终点
因rules所限,无法畅所欲言
北大侠客行MUD,中国最好的MUD |
|