当前位置: 首页 > news >正文

C#实现最短路径算法

 创建点集

            double r = 200 * 500;double width = 1920;double height = 1080;int col = (int)(r / width);int row = (int)(r / height);List<(double, double)> list1 = new List<(double, double)>();for (int i = 0; i < row; ++i){var y = i * height;if (y < r){var xxx = Math.Sqrt(r * r - y * y);var x = xxx - (xxx % width);list1.Add((x, y));list1.Add((-x, y));list1.Add((x, -y));list1.Add((-x, -y));}}

 点阵像这样一样

最短路径算法,使用LinkedList返回,后续对插入友好

  LinkedList<(double, double)> NearestNeighborTSP(List<(double, double)> points){int n = points.Count;bool[] visited = new bool[n];visited[0] = true;int current = 0;LinkedList<(double, double)> path = new LinkedList<(double, double)>();path.AddLast(points[current]);for (int i = 1; i < n; i++){double minDistance = double.MaxValue;int next = -1;for (int j = 0; j < n; j++){if (!visited[j]){double dist = Distance(points[current], points[j]);if (dist < minDistance){minDistance = dist;next = j;}}}current = next;visited[current] = true;path.AddLast(points[current]);}path.AddLast(points[0]);return path;}double Distance((double, double) point1, (double, double) point2){return Math.Sqrt(Math.Pow(point1.Item1 - point2.Item1, 2) + Math.Pow(point1.Item2 - point2.Item2, 2));}

 路径找完之后(局部展示图,斜线连起来的)

 矫正斜线

            var currentNode = res.First;while (currentNode != null && currentNode.Next != null){var nextNode = currentNode.Next;if (currentNode.Value.Item1 != nextNode.Value.Item1 && currentNode.Value.Item2 != nextNode.Value.Item2){var tempX = Math.Min(currentNode.Value.Item1, nextNode.Value.Item1);var tempY = currentNode.Value.Item1 > nextNode.Value.Item1 ? currentNode.Value.Item2 : nextNode.Value.Item2;res.AddAfter(currentNode, (tempX, tempY));currentNode = nextNode; // Skip the inserted node}elsecurrentNode = currentNode.Next;}

矫正后效果

完整测试代码(demo中所用WPF框架,图表控件为ScottPlot5,nuget里直接搜,装5.0以上版本):

public void test(){double r = 200 * 500;double width = 1920;double height = 1080;int col = (int)(r / width);int row = (int)(r / height);List<(double, double)> list1 = new List<(double, double)>();for (int i = 0; i < row; ++i){var y = i * height;if (y < r){var xxx = Math.Sqrt(r * r - y * y);var x = xxx - (xxx % width);list1.Add((x, y));list1.Add((-x, y));list1.Add((x, -y));list1.Add((-x, -y));}}var wpfPlot = new ScottPlot.WPF.WpfPlot();var xs = list1.Select(x => x.Item1).ToArray();var ys = list1.Select(y => y.Item2).ToArray();var xx = wpfPlot.Plot.Add.Scatter(xs, ys, ScottPlot.Colors.Red).LineWidth = 0;var res = NearestNeighborTSP(list1);var currentNode = res.First;while (currentNode != null && currentNode.Next != null){var nextNode = currentNode.Next;if (currentNode.Value.Item1 != nextNode.Value.Item1 && currentNode.Value.Item2 != nextNode.Value.Item2){var tempX = Math.Min(currentNode.Value.Item1, nextNode.Value.Item1);var tempY = currentNode.Value.Item1 > nextNode.Value.Item1 ? currentNode.Value.Item2 : nextNode.Value.Item2;res.AddAfter(currentNode, (tempX, tempY));currentNode = nextNode; // Skip the inserted node}elsecurrentNode = currentNode.Next;}var xs2 = res.Select(x => x.Item1).ToArray();var ys2 = res.Select(x => x.Item2).ToArray();var yy = wpfPlot.Plot.Add.Scatter(xs2, ys2, ScottPlot.Colors.Blue).LineWidth = 1;grid.Children.Add(wpfPlot);}LinkedList<(double, double)> NearestNeighborTSP(List<(double, double)> points){int n = points.Count;bool[] visited = new bool[n];visited[0] = true;int current = 0;LinkedList<(double, double)> path = new LinkedList<(double, double)>();path.AddLast(points[current]);for (int i = 1; i < n; i++){double minDistance = double.MaxValue;int next = -1;for (int j = 0; j < n; j++){if (!visited[j]){double dist = Distance(points[current], points[j]);if (dist < minDistance){minDistance = dist;next = j;}}}current = next;visited[current] = true;path.AddLast(points[current]);}path.AddLast(points[0]);return path;}double Distance((double, double) point1, (double, double) point2){return Math.Sqrt(Math.Pow(point1.Item1 - point2.Item1, 2) + Math.Pow(point1.Item2 - point2.Item2, 2));}
}

http://www.lryc.cn/news/398924.html

相关文章:

  • Python函数 之 匿名函数
  • 深入解析 Mybatis 中 Mapper 接口的实现原理
  • 微信小程序获取用户头像
  • uniapp小程序连接蓝牙设备
  • AI大模型推理过程与优化技术深度剖析
  • Dubbo 核心概念介绍
  • 练习 6.7:⼈们 在为练习 6.1 编写的程序中,再创建两个表⽰⼈的字典,然后将这三个字典都存储在⼀个名为 people 的列表中。
  • 星环科技知识平台TKH:引领企业构建高效AI基础设施,加速数智化转型新纪元
  • 嵌入式板级支持包(BSP)80道面试题及参考答案(3万字长文)
  • 如何找回误删的文件?4个常用文件恢复方法!
  • 在大型企业级应用中,如何优化 XML 数据的存储和检索效率,以满足高并发访问需求?
  • win10 A4000 下使用Xinference来进行大模型的推理测试
  • 【9-2:代码规范】
  • std::filesystem::current_path().generic_string()的bug
  • Python excel知识库批量模糊匹配的3种方法实例(fuzzywuzzy\Gensim)
  • stm32使用单通道规则组ADC
  • [python][whl]causal-conv1d的python模块在windows上whl文件下载
  • 介绍 CM3leon,一个更高效、最先进的文本和图像生成模型
  • HTTPS和HTTP有哪些区别
  • Docker 安装 PostgreSQL
  • 实践致知第12享:如何新建一个Word并设置格式
  • Rust vs Go: 特点与应用场景分析
  • 2024的开放式耳机排行榜,看这六个耳机选购的小Tips
  • JAVA-报表模糊搜索询易实现
  • 牛客 7.13 月赛(留 C逆元)
  • FPGA之术语
  • WPF透明置顶窗口wine适配穿透问题解决
  • 浅析Kafka Streams中KTable.aggregate()方法的使用
  • java word转pdf、word中关键字位置插入图片 工具类
  • jail内部ubuntu apt升级失败问题解决