查找矩形的算法

时间:2012-06-26 16:57:35

标签: 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');
    }
}
我正在寻找能够提取范围的算法,我们有一个。

所以根据这些数据我们会得到矩形 (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;
}

3 个答案:

答案 0 :(得分: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.
}  

`

我知道这段代码可能非常混乱,但这是你作为递归元素所需要的要点。  我还会注意到我已经看到了这段代码中的一些错误,但是我已经用完了15分钟,我说我将花在这篇文章上,所以你可能不得不自己选择它们。

答案 1 :(得分:2)

这为您提供了所需的相同结果:

static void Main(string[] args)
{
    string values =
        "1100000000" +
        "1100000011" +
        "0001100011" +
        "0001100000" +
        "0001110000" +
        "0000000110" +
        "0000000110";

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

    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            array[x, y] = (values[x + y * width] == '1');

    List<Rectangle> rectangles = new List<Rectangle>();

    for (int x = 0; x < width; ++x)
    {
        for (int y = 0; y < height; ++y)
        {
            if (array[x, y] && !Used(rectangles, x, y))
            {
                int rHeight = 1;
                for (int rX = x + 1; rX < width && array[rX, y] && !Used(rectangles, rX, y); ++rX)
                    for (int rY = y + 1; rY < height && array[rX, rY] && !Used(rectangles, rX, rY); ++rY)
                        if (rY - y >= rHeight)
                            rHeight = rY - y + 1;

                int rWidth = 1;
                for (int rY = y + 1; rY < height && rY - y <= rHeight && array[x, rY] && !Used(rectangles, x, rY); ++rY)
                    for (int rX = x + 1; rX < width && array[rX, rY] && !Used(rectangles, rX, rY); ++rX)
                        if (rX - x >= rWidth)
                            rWidth = rX - x + 1;

                rectangles.Add(new Rectangle(x, y, rWidth, rHeight));
            }
        }
    }

    foreach (Rectangle rect in rectangles)
        Console.WriteLine(rect);
}

private static bool Used(IEnumerable<Rectangle> rectangles, int x, int y)
{
    return rectangles.Any(r => r.Contains(x, y));
}

我创建了一个特殊的Rectangle结构,因为我没有引用System.Drawing,但你可以将System.Drawing.Point传递给System.Drawing.Rectangle.Contains()并获得相同的结果。

另外,请注意,数组的宽度实际应为10,并且索引数学是错误的。你应该将y乘以宽度而不是高度。

答案 2 :(得分:1)

从问题中不清楚你是否真的想要完全覆盖1的矩形,或者你是否想要包含零的边界体积,但是会用相当少量的矩形覆盖所有1个。

假设您希望矩形覆盖1,并且您不需要完美的解决方案:

  1. 制作数组的临时副本。
  2. 迭代暂时寻找1的
  3. 当您点击1时,开始一个以1x1开头的新rectagle,偏移到该位置(例如仅覆盖该位置)
  4. 只要下一个单元格中有1,就向右展开该矩形
  5. 只要下面的行的宽度与1匹配,就向下展开该矩形 当前矩形。
  6. ONce你不能再向下扩展,发出那个重新组合,并从临时中清除该矩形覆盖的所有1个
  7. 从当前矩形的右上角后面的单元格开始继续扫描1。
  8. 这将产生一个体面的覆盖 - 但绝不是理想的。如果你需要一个完美的覆盖物 - 例如保证最小数量的矩形然后更难。