当前位置: 首页 > 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));}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 记录些Redis题集(1)
  • mysql历史记录
  • Tomcat底层原理
  • 机器学习——关于极大似然估计法的一些个人思考(通俗易懂极简版)
  • 超详细版阿里云控制台环境配置+数据库配置
  • 电脑出现了msvcr120.dll丢失的问题要怎样修复?理性分析msvcr120.dll文件
  • C++基础入门(上)
  • 从零开始学习PX4源码3(如何上传官网源码到自己的仓库中)
  • 全渠道AI智能商品管理软件平台 助力零售品牌占领技术高地
  • Understanding EtherCAT Device Serial Number Checking
  • 图数据库 - Neo4j简介
  • Elasticsearch 8 支持别名查询
  • centos 安装vnc,配置图形界面
  • 学习测试8-数据库mysql操作
  • 基于SpringBoot+Vue的数码论坛系统(带1w+文档)
  • [NodeJS] 关于Buffer
  • Apache的基本使用
  • express + mock 让前后台并行开发
  • happypack两次报错的问题
  • Sequelize 中文文档 v4 - Getting started - 入门
  • text-decoration与color属性
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 构建二叉树进行数值数组的去重及优化
  • 后端_MYSQL
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 入门到放弃node系列之Hello Word篇
  • 深入浏览器事件循环的本质
  • 数组大概知多少
  • 在electron中实现跨域请求,无需更改服务器端设置
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​​​​​​​​​​​​​​Γ函数
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​草莓熊python turtle绘图代码(玫瑰花版)附源代码
  • #define,static,const,三种常量的区别
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (不用互三)AI绘画工具应该如何选择
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (过滤器)Filter和(监听器)listener
  • (一) 初入MySQL 【认识和部署】
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .describe() python_Python-Win32com-Excel
  • .libPaths()设置包加载目录
  • .NET C# 配置 Options
  • .NET Standard 的管理策略
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • .one4-V-XXXXXXXX勒索病毒数据怎么处理|数据解密恢复
  • .stream().map与.stream().flatMap的使用
  • /run/containerd/containerd.sock connect: connection refused
  • [ 手记 ] 关于tomcat开机启动设置问题
  • [2024最新教程]地表最强AGI:Claude 3注册账号/登录账号/访问方法,小白教程包教包会
  • [ACP云计算]易混淆知识点(考题总结)
  • [AIGC] 如何建立和优化你的工作流?
  • [Android Studio] 开发Java 程序