Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在数组中寻找正方形_Vb.net_Arrays_Algorithm - Fatal编程技术网

Vb.net 在数组中寻找正方形

Vb.net 在数组中寻找正方形,vb.net,arrays,algorithm,Vb.net,Arrays,Algorithm,我有一个二维数组,里面充满了1和0,比如 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 你可以看到在th数组中有一个正方形。 我正在尝试创建一个函数,该函数将基于正方形生成一个矩形或矩形列表。 因此,示例将返

我有一个二维数组,里面充满了1和0,比如

    0 0 0 0 0 0 0 0 0 0
    0 0 1 1 1 1 1 1 0 0
    0 0 1 0 0 0 0 1 0 0
    0 0 1 0 0 0 0 1 0 0
    0 0 1 0 0 0 0 1 0 0
    0 0 1 1 1 1 1 1 0 0
    0 0 0 0 0 0 0 0 0 0
你可以看到在th数组中有一个正方形。 我正在尝试创建一个函数,该函数将基于正方形生成一个矩形或矩形列表。 因此,示例将返回一个矩形,如

rect.x = 2
rect.y = 1
rect.width = 7
rect.height = 5
这是我现在拥有的代码,但它没有返回任何内容

Dim rects As New List(Of Rectangle)
    For imgWidth As Integer = 0 To bow.GetUpperBound(0)
        For imgHeight As Integer = 0 To bow.GetUpperBound(1)
            If bow(imgWidth, imgHeight) = 1 Then

                If bow(imgWidth + 1, imgHeight) = 1 And 
                   bow(imgWidth + 2, imgHeight) = 1 And 
                   bow(imgWidth, imgHeight + 1) = 1 And 
                   bow(imgWidth, imgHeight + 2) = 1 Then

                    Dim r As New Rectangle

                    With r
                        .X = imgWidth
                        .Y = imgHeight
                    End With

                    For rectWidth As Integer = imgWidth To bow.GetUpperBound(0)
                        If bow(rectWidth, imgHeight) = 0 Then
                            r.Width = bow(rectWidth - 1, imgHeight)
                        End If
                    Next

                    For rectHeight As Integer = imgHeight To bow.GetUpperBound(1)
                        If bow(imgWidth, rectHeight) = 0 Then
                            r.Height = bow(rectHeight - 1, imgHeight)
                        End If
                    Next

                    rects.Add(r)
                End If
            End If
        Next
    Next

此外,阵列必须能够有多个正方形。

我会这样做:

def rectangles(grid):
  rows = len(grid)
  cols = len(grid[0])

  hor_ones = [[0]] * rows
  for r in range(rows):
    for c in range(cols):
      hor_ones[r].append(hor_ones[r][c] + grid[r][c])

  ver_ones = [[0]] * cols
  for c in range(cols):
    for r in range(rows):
      ver_ones[c].append(ver_ones[c][r] + grid[r][c])

  ret = []
  for r1 in range(rows):
    for c1 in range(cols):
      for r2 in range(r1+1, rows):
        for c2 in range(c1+1, cols):
          if all_ones(hor_ones[r1], c1, c2) and all_ones(hor_ones[r2], c1, c2) and all_ones(ver_ones[c1], r1, r2) and all_ones(ver_ones[c2], r1, r2):
            ret.append((r1, c2, r2, c2))
  return ret

def all_ones(ones, x, y):
  return ones[y+1] - ones[x] == y - x + 1
请注意:

  • hor_ones[r][c]
    是 第一个c列中r行中的
  • ver_ones[c][r]
    是 前r行c列中的
因此,行
r
和列
c1
c2
(含)之间的数字为:

hor_-one[r][c2+1]-hor_-one[r][c1]

编辑

这是我用Java编写的解决方案,也许在VB.NET中更容易理解和实现:

public static List<Rectangle> findRectangles(int[][] grid) {
    int rows = grid.length;
    int cols = grid[0].length;

    int[][] horOnes = new int[rows][cols+1];
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            horOnes[r][c+1] = horOnes[r][c] + grid[r][c];

    int[][] verOnes = new int[cols][rows+1];
    for (int c = 0; c < cols; c++)
        for (int r = 0; r < rows; r++)
            verOnes[c][r+1] = verOnes[c][r] + grid[r][c];

    List<Rectangle> ret = new ArrayList<Rectangle>();
    for (int r1 = 0; r1 < rows; r1++)
        for (int c1 = 0; c1 < cols; c1++)
            for (int r2 = r1+1; r2 < rows; r2++)
                for (int c2 = c1+1; c2 < cols; c2++)
                    if (allOnes(horOnes[r1], c1, c2) && allOnes(horOnes[r2], c1, c2) && allOnes(verOnes[c1], r1, r2) && allOnes(verOnes[c2], r1, r2))
                        ret.add(new Rectangle(r1, c1, r2, c2));
    return ret;
}

private static boolean allOnes(int[] ones, int x, int y) {
    return ones[y+1] - ones[x] == y - x + 1;
}
公共静态列表findRectangles(int[][]网格){
int rows=grid.length;
int cols=网格[0]。长度;
int[][]horOnes=新的int[行][cols+1];
对于(int r=0;r
除了“它只是不返回任何东西”之外,您还可以提供问题的更多细节吗?你尝试过什么?当我尝试添加断点时,找到宽度和高度的循环总是返回0@giodamelio这是家庭作业吗?如果不是,它是用来做什么的?为什么?如果二维数组(7x9)是满的呢?它应该返回756个可能的矩形列表吗?@JoshD不,它不是家庭作业,因为某种原因,它被自动标记。数组是从一个文件中读取的,该文件是由一个不同的程序创建的,该程序最初从一个拖放界面创建该文件,用于在画布上放置正方形。非常感谢,但有人能将其转换为vb.net。我真的不知道该如何处理它。@giodmaelio这是python,它是一种比vb更好的学习语言。引用一句话,“……基础知识的教学应该被定为刑事犯罪:它会使人的大脑无法恢复。”你会注意到,解决你问题的人不是VB程序员。@Sheldon L.Cooper-干得好!不幸的是,矩形的构造函数采用宽度和高度,而不是右下角,因此需要修复实例化。此外,您的horOnes和verOnes不会重置为零-请参阅我的答案以获得修复。@Sheldon L.Cooper-好的,我假设您使用的是标准Java矩形。不管怎样,我相信如果你打印出verOnes或horOnes的结果,你会发现它错误地延伸到了右边和底部,从而创建了额外的矩形。