科大讯飞笔试题 20220820
科大讯飞 笔试第三题:
#科大讯飞# #科大讯飞笔试#
外星人探索虫洞飞行,
大概意思是说,构造一个有向图,从某个节点出发,某些边可以连续前进,计算连续前进的长度
输入:
8
1,3,True
1,4,False
3,5,True
4,5,True
4,7,False
5,6,True
6,2,False
7,2,True
 
第一行为边的个数
后面的所有行是 起点 终点 是否能够通行。
结果
1 3
2 0
3 2
4 2
5 1
6 0
7 1
输出结果为,
节点编号,每个节点可以前进的长度
 
python 代码
 
 
#科大讯飞##科大讯飞笔试#
   #科大讯飞# #科大讯飞笔试#
外星人探索虫洞飞行,
大概意思是说,构造一个有向图,从某个节点出发,某些边可以连续前进,计算连续前进的长度
输入:
8
1,3,True
1,4,False
3,5,True
4,5,True
4,7,False
5,6,True
6,2,False
7,2,True
第一行为边的个数
后面的所有行是 起点 终点 是否能够通行。
结果
1 3
2 0
3 2
4 2
5 1
6 0
7 1
输出结果为,
节点编号,每个节点可以前进的长度
python 代码
from ast import Num
num = 8
n_list = [(1,3,True),
            (1,4,False),
            (3,5,True),
            (4,5,True),
            (4,7,False),
            (5,6,True),
            (6,2,False),
            (7,2,True)]
n_list_true={}
points = []
for i1,i2,enable in n_list:
    if enable:
        if i1 not in n_list_true.keys():
            n_list_true[i1] = []
        n_list_true[i1].append(i2)
    points.append(i1)
    points.append(i2)
points = sorted(set(points))
def dfs(p):
    if p in n_list_true.keys():
        max_r = 0
        for q in n_list_true[p]:
            max_r =max(max_r,1+dfs(q))
        return max_r
    return 0 
result = {}
for p in points:
    print(p,dfs(p))
 #科大讯飞##科大讯飞笔试#
查看7道真题和解析