Menu
CoddyTech

最终挑战第 1 题

Coddy 广度优先搜索:图算法 课程的第 8 课,共 9 课。

challenge icon

挑战

中等

现在来看看 BFS 的典型用法。

编写一个名为 shortestPath 的函数,它接收 n、扁平的 edges 数组(无向图)、start 顶点和 target 顶点,并返回从 start 到 target 的最短路径长度(边的数量)。如果无法到达 target,则返回 -1。

例如,在路径 0-1-2-3 中,从 0 到 3 的最短路径长度为 3。

亲手试试

#include <stdlib.h>

int shortestPath(int n, int* edges, int edges_size, int start, int target) {
    // 在这里编写代码
    return -1;
}

广度优先搜索:图算法中的所有课

自己练习: C 在线编译器