首页 > 编程语言 >C# 找到给定点集的简单闭合路径(Find Simple Closed Path for a given set of points)

C# 找到给定点集的简单闭合路径(Find Simple Closed Path for a given set of points)

时间:2024-09-18 13:52:46浏览次数:3  
标签:p2 given set Point C# p1 int points new

 给定一组点,将这些点连接起来而不相交

例子: 

输入:points[] = {(0, 3), (1, 1), (2, 2), (4, 4),
                   (0, 0), (1, 2), (3, 1}, {3, 3}};

输出:按以下顺序连接点将
        不造成任何交叉
       {(0, 0), (3, 1), (1, 1), (2, 2), (3, 3),
        (4,4),(1,2),(0,3)}
        
我们强烈建议您最小化浏览器并先自己尝试一下。

这个想法是使用排序。 

通过比较所有点的 y 坐标来找到最底部的点。如果有两个点的 y 值相同,则考虑 x 坐标值较小的点。将最底部的点放在第一个位置。 

考虑剩余的 n-1 个点,并围绕 points[0] 按照极角逆时针顺序排列它们。如果两个点的极角相同,则将最近的点放在最前面。

遍历排序数组(按角度升序排序)产生简单的闭合路径。

如何计算角度? 
一种解决方案是使用三角函数。 
观察:我们不关心角度的实际值。我们只想按角度排序。 
想法:使用方向来比较角度,而无需实际计算它们!

以下是上述想法的实现:

using System;
using System.Collections.Generic;
 
public class Point {
    public int x, y;
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
public class ClosestPath {
    static Point p0;
 
    static int dist(Point p1, Point p2)
    {
        return (p1.x - p2.x) * (p1.x - p2.x)
            + (p1.y - p2.y) * (p1.y - p2.y);
    }
 
    static int orientation(Point p, Point q, Point r)
    {
        int val = (q.y - p.y) * (r.x - q.x)
                  - (q.x - p.x) * (r.y - q.y);
        if (val == 0)
            return 0; // collinear
        return (val > 0)
            ? 1
            : 2; // clockwise or counterclockwise
    }
 
    static int compare(Point p1, Point p2)
    {
        int o = orientation(p0, p1, p2);
        if (o == 0)
            return (dist(p0, p2) >= dist(p0, p1)) ? -1 : 1;
        return (o == 2) ? -1 : 1;
    }
 
    static void printClosedPath(List<Point> points, int n)
    {
        // Find the bottommost point
        int ymin = points[0].y;
        int min = 0;
        for (int i = 1; i < n; i++) {
            int y = points[i].y;
            if ((y < ymin)
                || (ymin == y
                    && points[i].x < points[min].x)) {
                ymin = points[i].y;
                min = i;
            }
        }
 
        // Place the bottom-most point at first position
        Point temp = points[0];
        points[0] = points[min];
        points[min] = temp;
 
        // Sort n-1 points with respect to the first point.
        // A point p1 comes before p2 in sorted output if p2
        // has larger polar angle (in counterclockwise
        // direction) than p1
        p0 = points[0];
        points.Sort(compare);
 
        // Now stack has the output points, print contents
        // of stack
        for (int i = 0; i < n; i++) {
            Console.Write("(" + points[i].x + ", "
                          + points[i].y + "), ");
        }
    }
 
    public static void Main()
    {
        List<Point> points = new List<Point>() {
            new Point(0, 3), new Point(1, 1),
                new Point(2, 2), new Point(4, 4),
                new Point(0, 0), new Point(1, 2),
                new Point(3, 1), new Point(3, 3)
        };
        int n = points.Count;
 
        printClosedPath(points, n);
    }
}
// This code is contributed by user_dtewbxkn77n 

 输出: 

(0, 0), (3, 1), (1, 1), (2, 2), (3, 3),
(4,4),(1,2),(0,3),
如果我们使用 O(nLogn) 排序算法对点进行排序,则上述解决方案的时间复杂度为 O(n Log n)。
辅助空间: O(1),因为没有占用额外空间。

来源: 
http://www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf

标签:p2,given,set,Point,C#,p1,int,points,new
From: https://blog.csdn.net/hefeng_aspnet/article/details/141715914

相关文章

  • Spring Cloud 5.2: 将多工程整合成多模块工程-gateway
    书接上回,参照上一篇"移植"Eureka的套路,将gateway工程"移植"到模块中移植方式跟eureka一样,这里不过多赘述,注意这几步就好:1.build.gradle2.application.yml3.GatewayApplication:入口类的名称规则为模块名(ArtifactId)+Application,由于我移植时模块名与原工程名不同,所以做了改动。4.......
  • Nature Genetics | Rajeev K. Varshney综述:解锁植物遗传学的端粒到端粒(T2T)基因组组装
    近期,RajeevK.Varshney团队在Naturegenetics发表综述文章:Unlockingplantgeneticswithtelomere-to-telomeregenomeassemblies。摘要连续基因组序列组装将帮助我们实现作物转化基因组学的全面潜力。最近在测序技术方面的进步,尤其是长读长测序策略,使得构建无间隙的端粒到端粒(T......
  • Cell Reports | 下一代混池测序技术(NG-BSA)助力育种4.0
    分享一篇去年由中国农科院作物所张红伟和华中农业大学李林团队发表在CellReports上的综述文章:Next-generationbulkedsegregantanalysisforBreeding4.0。该综述在总结传统混池测序技术(BSA)的基础上,提出了NG-BSA的研究策略。NG-BSA通过整合高通量表型技术、生物大数据技术与机......
  • ECharts图表图例
    数据可视化用eclipse软件java代码:<!DOCTYPEhtml><!DOCTYPENLMI><html><head><metacharset="UTF-8"><!--引入ECharts脚本--><scriptsrc="js/echarts.js"></script><title>绘制堆积柱状图</title......
  • C#------LINQ查询(一)
    1.查询一定范围数字staticvoidQueryInt(){//Specifythedatasource.int[]scores={97,92,81,60};//Definethequeryexpression.IEnumerable<int>scoreQuery=froms......
  • 【每日一题】LeetCode 2332.坐上公交的最晚时间(数组、双指针、二分查找、排序)
    【每日一题】LeetCode2332.坐上公交的最晚时间(数组、双指针、二分查找、排序)题目描述给你一个下标从0开始长度为n的整数数组buses,其中buses[i]表示第i辆公交车的出发时间。同时给你一个下标从0开始长度为m的整数数组passengers,其中passengers[j]表示第......
  • Nature Comm. | CoPheScan:一种考虑连锁不平衡的全表型组关联分析
    分享一篇最近发表在NC的一篇文章:CoPheScan:phenome-wideassociationstudiesaccountingforlinkagedisequilibrium。文章介绍了一种新的贝叶斯方法CoPheScan(ColocadaptedPhenome-wideScan),用于在考虑连锁不平衡(LD)的情况下进行表型范围关联研究(Phenome-wideassociationstud......
  • 56.【C语言】字符函数和字符串函数(strtok函数)(未完)
    目录12.strtok函数(较复杂)*简单使用总结:*优化12.strtok函数(较复杂)*简单使用strtok:stringintotokenscplusplus的介绍点我跳转翻译:函数strtokchar*strtok(char*str,constchar*delimiters);总结:delimiters参数指向一个字符串,定义了用......
  • Excel歪门邪道(1) — 用C#解决Excel修复文件后批注被删除的问题
    今天是2024年9月18日,中秋节刚过,狠下心来决定把原来发在CSDN的文章全部搬过来,倒不是因为文章被CSDN拿去白嫖或者是担心CSDN倒闭,而是觉得自己过去的29年生活自己活的像个小丑,如今已到而立之年被社会彻底淘汰已成定局,因此在离开这个世界之前,我还是决定把过往的一些没什么用的经验重新......
  • 2024年一站式解决用termux安装matplotlib,pandas,numy和scipy问题
    用Python玩数据的技术人员都知道这几个库的重要性,话不多说,直接开始! termux版本:0.119.0-bate1​​​​   1.安装numpynumpy是Python的一种开源的科学计算库现在安装的是最新版本 1.26.5它是安装这几个库中最简单的,只需键入:pkgupdate&&pkgupgrade  #养......