【问题标题】:Given a set of lattice points, how many group of points are there?给定一组格点,有多少组点?
【发布时间】:2020-01-19 13:48:28
【问题描述】:

这个想法是给定平面上的一组格点,确定给定集合中有多少“组”点。 一组点定义如下:

Given a set S of lattice points, 
it is said that these points form a group of points if and only if:
for each a of S the distance of the nearest point(s) is 1

该函数必须返回一个包含所有现有点组的列表。

input: --> point: list
output: --> group: list

如果有可能获得更好的算法,因为我不确定这段代码是否适用于每组点。

我的代码是这样的

def walk_point_path(points):
    groups = []
    points_x = sorted(points, key=lambda x: x[1])
    visited = [points_x[0]]
    q = [points_x[0]]
    while points_x:
        while q:
            x, y = q.pop()
            for x, y in (x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y):
                if [x, y] not in visited and [x, y] in points_x:
                    q.append([x, y])
                    visited.append([x, y])
        groups.append(visited)
        for point in visited:
            points_x.remove(point)
        if len(points_x) > 0:
            q = [points_x[0]]
            visited = [points_x[0]]
    return groups

【问题讨论】:

  • 在一维示例中,如果您有点 {1, 2, 5, 6}。这是一个单独的组(因为您的条件对所有点都是正确的)还是两个单独的组?你的晶格是均匀的和单位间距的(即,你有整数坐标吗)?
  • @NicoSchertler 在一维示例的情况下,集合 {1,2,5,6} 包含两组点。而格点空间就像你说的uniform和unit-spaced,所有要测试的点都有整数坐标

标签: algorithm geometry computational-geometry


【解决方案1】:

考虑connected-components labeling 算法的一些好的实现。

您当前的方法利用 Flood-fill 算法 (One component at a time kind) 来分组获得积分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    相关资源
    最近更新 更多