5

我有以下代码:

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');
    }
}

我正在寻找一种算法来提取我们有 1 的范围。

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

但我不知道如何做到这一点任何人有任何指示?

阅读生锈的韦伯回答后,我想出了以下内容:

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;
}
4

3 回答 3

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 分钟,所以你可能不得不自己挑选它们。

于 2012-06-26T17:06:30.133 回答
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));
}

因为我没有引用 System.Drawing,所以我制作了一个临时 Rectangle 结构,但是您可以将 System.Drawing.Point 传递给 System.Drawing.Rectangle.Contains() 并获得相同的结果。

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

于 2012-06-26T20:15:31.503 回答
1

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

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

  1. 制作数组的临时副本。
  2. 遍历临时寻找 1
  3. 当您点击 1 时,开始一个新的矩形,以 1x1 开头,偏移到该位置(例如仅覆盖该 1 )
  4. 只要下一个单元格中有 1,就向右扩展该矩形
  5. 只要下面的行与当前矩形的宽度匹配,向下扩展该矩形。
  6. 一旦你不能再向下扩展,发出那个矩形,并从临时清除该矩形覆盖的所有 1
  7. 继续从当前矩形右上角之后的单元格开始扫描 1。

这将产生一个不错的覆盖 - 但绝不是理想的。如果您需要一个完美的覆盖 - 例如保证最小数量的矩形,那就更难了。

于 2012-06-26T18:01:44.157 回答