You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET应用中基于BFS算法实现两点间线路路径查找?

路由路径查找实现问题

路由系统示意图

这是关于路由系统应用的技术问题,承接之前的提问。已了解相关概念,但无法将理论转化为代码。需求简化为:查找从Point A到E的所有线路,结果:L1, L3, L6, L7, L8(基于上图)。

核心需求

  • 需获取所有可能路径(多数情况下仅存在一条);
  • A到E与E到A路径等效,但返回的线路顺序需满足追踪需求;
  • 提供的WinForm示例仅作演示,无需图形化实现。

实现卡点

已知需基于graph实现BFS算法,尝试参考BFS路径查找示例,但卡在如何将输入从示例中的List<integer>改为自定义的List<Line>。

待实现方法:

List<List<Line>> FindPaths(List<Line> InputLines, Point src, Point des)
{
}
// var A = myList[0].P1; 
// var E = myList[7].P2; 

// var paths = FindPaths(myList,A, E);

已定义类及示例代码

Line类定义

public class Line
{
    public string Name { get; set; }
    public Point P1 { get; set; }
    public Point P2 { get; set; }

    public Line(string name, Point p1, Point p2)
    {
        Name = name;
        P1 = p1;
        P2 = p2;
    }
}

点与线路示例代码

List<Line> myList;
private void DrawSample()
{
    //Point definitions
    var L1_P1 = new Point(40, 40); //Point A
    var L1_P2 = new Point(100, 100);

    var L2_P1 = new Point(100, 100);
    var L2_P2 = new Point(40, 160); //Point B

    var L3_P1 = new Point(100, 100);
    var L3_P2 = new Point(180, 100);

    var L4_P1 = new Point(180, 100);
    var L4_P2 = new Point(180, 25); //Point C

    var L5_P1 = new Point(180, 100);
    var L5_P2 = new Point(220, 165); //Point D

    var L6_P1 = new Point(180, 100);
    var L6_P2 = new Point(260, 100);

    var L7_P1 = new Point(260, 100);
    var L7_P2 = new Point(350, 100);

    var L8_P1 = new Point(350, 100);
    var L8_P2 = new Point(480, 100);//Point E

    //Line definitions
    var L1 = new Line("L1", L1_P1, L1_P2);
    var L2 = new Line("L2", L2_P1, L2_P2);
    var L3 = new Line("L3", L3_P1, L3_P2);
    var L4 = new Line("L4", L4_P1, L4_P2);
    var L5 = new Line("L5", L5_P1, L5_P2);
    var L6 = new Line("L6", L6_P1, L6_P2);
    var L7 = new Line("L7", L7_P1, L7_P2);
    var L8 = new Line("L8", L8_P1, L8_P2);

    myList = new List<Line>();
    myList.Add(L1);
    myList.Add(L2);
    myList.Add(L3);
    myList.Add(L4);
    myList.Add(L5);
    myList.Add(L6);
    myList.Add(L7);
    myList.Add(L8);

    //Graphic
    var brush = new SolidBrush(Color.Blue);
    var pen = new Pen(Color.Black, 2);

    //panelCanvas is just a WinForm Panel
    var canvas = panelCanvas.CreateGraphics();

    canvas.DrawLine(pen, L1.P1, L1.P2); //L1
    canvas.DrawLine(pen, L2.P1, L2.P2); //L2
    canvas.DrawLine(pen, L3.P1, L3.P2); //L3
    canvas.DrawLine(pen, L4.P1, L4.P2); //L4
    canvas.DrawLine(pen, L5.P1, L5.P2); //L5
    canvas.DrawLine(pen, L6.P1, L6.P2); //L6
    canvas.DrawLine(pen, L7.P1, L7.P2); //L7
    canvas.DrawLine(pen, L8.P1, L8.P2); //L8

    canvas.DrawString("L1", new Font("Tohoma", 10), brush, 70, 50);
    canvas.DrawString("L2", new Font("Tohoma", 10), brush, 70, 140);
    canvas.DrawString("L3", new Font("Tohoma", 10), brush, 132, 80);
    canvas.DrawString("L4", new Font("Tohoma", 10), brush, 184, 60);
    canvas.DrawString("L5", new Font("Tohoma", 10), brush, 180, 135);
    canvas.DrawString("L6", new Font("Tohoma", 10), brush, 220, 80);
    canvas.DrawString("L7", new Font("Tohoma", 10), brush, 300, 80);
    canvas.DrawString("L8", new Font("Tohoma", 10), brush, 420, 80);

    //Markings
    canvas.DrawString("X", new Font("Tohoma", 10, FontStyle.Bold), brush, 94, 92);
    canvas.DrawString("X", new Font("Tohoma", 10, FontStyle.Bold), brush, 174, 92);
    canvas.DrawString("X", new Font("Tohoma", 10, FontStyle.Bold), brush, 260, 92);
    canvas.DrawString("X", new Font("Tohoma", 10, FontStyle.Bold), brush, 350, 92);

    canvas.DrawString("A", new Font("Tohoma", 10, FontStyle.Bold), new SolidBrush(Color.Red), 32, 25);
    canvas.DrawString("B", new Font("Tohoma", 10, FontStyle.Bold), new SolidBrush(Color.Red), 40, 160);

    canvas.DrawString("C", new Font("Tohoma", 10, FontStyle.Bold), new SolidBrush(Color.Red), 174, 10);
    canvas.DrawString("D", new Font("Tohoma", 10, FontStyle.Bold), new SolidBrush(Color.Red), 220, 165);
    canvas.DrawString("E", new Font("Tohoma", 10, FontStyle.Bold), new SolidBrush(Color.Red), 480, 92);
}

内容的提问来源于stack exchange,提问作者N.TW12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 08:45:37