【问题标题】:how to find non intersecting bounding rectangles in the simple matrix in java?如何在java中的简单矩阵中找到不相交的边界矩形?
【发布时间】:2019-12-02 12:42:27
【问题描述】:

我有一个二维矩阵,看起来像

matr=([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]])

我需要编写 Java 代码来查找值 1 的非相交边界矩形

这里发布了一个类似的问题: Fill Bounding Boxes in 2D array

但是解决方案是在 Python 上使用一些特殊的库,我需要在 Java 中实现这个,另外我需要在这种情况下找到不相交的框

假设我们可以将点和矩形存储为

class Point{
    int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Rectangle{
    Point topLeft, bottomRight;

    public Rectangle(Point topLeft, Point bottomRight) {
        this.topLeft = topLeft;
        this.bottomRight = bottomRight;
    }
}

在上面提供的示例中,答案应该是 Rectangle(Point(2,2), Point(5,4)) 其他矩形相交,这就是为什么不需要计算它们

【问题讨论】:

  • 您能否详细说明一下 - 在这种情况下我需要找到不相交的框。如果您能举一些与应该忽略的相交框相关的示例,我将不胜感激。

标签: java algorithm sparse-matrix bounding-box


【解决方案1】:

您可以使用BFS (Breadth-first search) 算法来执行此操作。

步骤如下:

  1. 遍历所有点(矩阵中的所有值)并检查一个点是否具有值1
  2. 从包含1的非访问点开始
  3. 运行 BFS 以查找从该点开始的所有连接点。
  4. 标记/标记所有访问点。
  5. 计算连接点之间的Top Left点和Bottom Right点。
  6. 从 2 开始。

【讨论】:

    猜你喜欢
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多