‘‘‘常见数据结构-图‘‘‘ ‘‘‘a指向b,a指向d,依次类推‘‘‘ charts = {‘a‘:[‘b‘,‘d‘],‘c‘:[‘e‘],‘d‘:[‘c‘,‘e‘]} ‘‘‘遍历图中的路径‘‘‘ def path(chart,x,y,pathd=[]): pathd = pathd + [x] if x == y: return pathd if not chart.has_key(x): return None for jd in chart[x]: if jd not in pathd: newjd =path(chart,jd,y,pathd) if newjd: return newjd print(path(charts,‘a‘,‘e‘))
时间: 2024-10-03 04:36:10