> 文章列表 > 七段码蓝桥杯python解法(DFS)

七段码蓝桥杯python解法(DFS)

七段码蓝桥杯python解法(DFS)

题目描述

解题思路

其实和从某点出发的迷宫路径一样,只不过这个只要字母一样就都算一个比如bcd和cbd都算一个所以最后要排序set一下。直接用dfs遍历求解

代码

    # 初始化数据dict = {'a': ['f', 'b'], 'b': ['a', 'c', 'g'], 'c': ['b', 'd', 'g'],'d': ['e', 'c'], 'e': ['d', 'f', 'g'], 'f': ['a', 'e', 'g'],'g': ['b', 'c', 'e', 'f']}judge = set()result = 0def dfs(i):global resultif i not in judge:judge.add(i)result+=1else:returnfor d in dict[i[-1]]:if d not in i:dfs(''.join(i + d))returnfor i in ['a', 'b', 'c', 'd', 'e', 'f', 'g']:dfs(i)a=list(judge)b=[]for i in a:b.append(''.join(sorted(i)))print(len(set(b)))