【发布时间】: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