查找矩形的算法

Pet*_*ter 5 c# algorithm

我有以下代码:

int width = 10;
int height = 7;
bool[,] array1 = new bool[width, height];


string values = 
    "1100000000" +
    "1100000011" +
    "0001100011" +
    "0001100000" +
    "0001110000" +
    "0000000110" +
    "0000000110";

for (int x = 0; x < width; x++)
{
    for (int y = 0; y < height; y++)
    {
        array1[x, y] = (values[x + y * width] == '1');
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种能够提取范围的算法,我们有一个1.

所以从这个数据我们得到矩形(0,0,2,2),(8,1,2,2),(3,2,3,3),(7,5,2,2)的顺序矩形没关系!

但我不知道怎么做这个任何人有任何指针?

在阅读Rusty Weber回答后,我想出了以下内容:

private static List<Rectangle> GetRectangles(bool[,] array)
{
    List<Rectangle> rectangles = new List<Rectangle>();
    for (int x = 0; x < array.GetLength(0); x++)
    {
        for (int y = 0; y < array.GetLength(1); y++)
        {
            if (array[x, y])
            {
                rectangles.Add(GetRectangle(array, new Point(x, y)));
            }
        }
    }
    return rectangles;
}



static Rectangle GetRectangle(bool[,] array, Point startLocation)
{
    int maxX = int.MinValue;
    int minX = int.MaxValue;
    int maxY = int.MinValue;
    int minY = int.MaxValue;
    HashSet<Point> visitedLocations = new HashSet<Point>();
    Stack<Point> pointsToGo = new Stack<Point>();
    Point location;
    pointsToGo.Push(startLocation);
    while (pointsToGo.Count > 0)
    {
        location = pointsToGo.Pop();

        if (!location.X.IsBetween(0, array.GetLength(0) - 1))
            continue;
        if (!location.Y.IsBetween(0, array.GetLength(1) - 1))
            continue;
        if (!array[location.X, location.Y])
            continue;
        if (visitedLocations.Contains(location))
            continue;
        visitedLocations.Add(location);

        pointsToGo.Push(new Point(location.X + 1, location.Y));
        pointsToGo.Push(new Point(location.X, location.Y + 1));
        pointsToGo.Push(new Point(location.X - 1, location.Y));
        pointsToGo.Push(new Point(location.X, location.Y - 1));
    }

    foreach (Point location2 in visitedLocations)
    {
        array[location2.X, location2.Y] = false;
        if (location2.X > maxX)
            maxX = location2.X;
        if (location2.X < minX)
            minX = location2.X;
        if (location2.Y > maxY)
            maxY = location2.Y;
        if (location2.Y < minY)
            minY = location2.Y;
    }

    return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
}

public static bool IsBetween<T>(this T item, T start, T end)
{
    return Comparer<T>.Default.Compare(item, start) >= 0
        && Comparer<T>.Default.Compare(item, end) <= 0;
}
Run Code Online (Sandbox Code Playgroud)

Rus*_*ber 2

评论:如果您有更好的定义坐标,这可能会帮助我回答您的问题。(0,0,2,2) 并不完全是笛卡尔坐标,可能需要一些解释。这是左上角后面跟着宽度吗?

好的。至少在我看来,从图中提取所有可能的矩形的最简单的编程方法是使用递归定义的方法,在特定方向上搜索对称矩形图案。然而,这最终可能会非常慢,所以我希望速度不会成为您的限制。看看代码的风格,我想说这是递归或动态编程的学校作业。

类似于以下伪代码的内容

`

for i in width  
{  
for j in height  
{  
if(point[i,j] == 1)  
{  
       potentials = searh_in_direction(i,j,graph,width,height,RIGHT,[[i,j]] )  
     listOfAllRects.append(potentials)  
}  
}  
}
list_of_rectangle searh_in_direction(i,j,graph,width,height,direction, listofpoints )  
{  
  nextdirection = direction.nextdirection; //Right -> down -> left-> up 


  //DEVELOP METHOD FOR RECURSION HERE THAT RETURNS ALL SETS OF 4 POINTS THAT
  for every point in the direction of travel
  if the point is the origional point and we have 4 points including the point we are looking at, we have a rectangle and we need to return
  if point on direction of travel is a one travel on the next direction
  posiblerects.append(searh_in_direction(i,j,graph,width,height,nextdirection , listofpoints.append(currentpoint)))

//after all points in direction have bee searched
return posiblerects.
}  
Run Code Online (Sandbox Code Playgroud)

`

我知道这段代码可能会非常令人困惑,但这就是您作为递归元素所需的要点。我还要指出的是,我已经在这段代码中发现了几个错误,但是我已经用完了我所说的花在这篇文章上的 15 分钟时间,所以你可能必须自己找出它们。

  • 我已将您的回答作为对主要问题的评论。在获得大量反对票之前,您可能需要删除此答案。 (4认同)