mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Reformat the C# codes.
Disable creating new line before open brace.
This commit is contained in:
@ -9,12 +9,10 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_graph;
|
||||
|
||||
public class graph_bfs
|
||||
{
|
||||
public class graph_bfs {
|
||||
/* 广度优先遍历 BFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
public static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet)
|
||||
{
|
||||
public static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
@ -23,14 +21,11 @@ public class graph_bfs
|
||||
Queue<Vertex> que = new Queue<Vertex>();
|
||||
que.Enqueue(startVet);
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (que.Count > 0)
|
||||
{
|
||||
while (que.Count > 0) {
|
||||
Vertex vet = que.Dequeue(); // 队首顶点出队
|
||||
res.Add(vet); // 记录访问顶点
|
||||
foreach (Vertex adjVet in graph.adjList[vet])
|
||||
{
|
||||
if (visited.Contains(adjVet))
|
||||
{
|
||||
foreach (Vertex adjVet in graph.adjList[vet]) {
|
||||
if (visited.Contains(adjVet)) {
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
que.Enqueue(adjVet); // 只入队未访问的顶点
|
||||
@ -43,8 +38,7 @@ public class graph_bfs
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.ValsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
Vertex[][] edges = new Vertex[12][]
|
||||
@ -64,4 +58,4 @@ public class graph_bfs
|
||||
Console.WriteLine("\n广度优先遍历(BFS)顶点序列为");
|
||||
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user