Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:12:44+00:00 2026-05-17T17:12:44+00:00

I have an 2 dimensional array that is full of 1s and 0s such

  • 0

I have an 2 dimensional array that is full of 1s and 0s such as

    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

You can see there is a square in th array.
I am trying to make a function that will make a rectangle or list of rectangles based on the square.
So the example would return a rectangle like

rect.x = 2
rect.y = 1
rect.width = 7
rect.height = 5

This is the code i have now but it just doe not return anything

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

Also the array must be able to have more than one square.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:12:45+00:00Added an answer on May 17, 2026 at 5:12 pm

    This is how I would do it:

    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
    

    Note that:

    • hor_ones[r][c] is the number of
      ones in row r in the first c columns.
    • ver_ones[c][r] is the number of
      ones in column c in the first r rows.

    Therefore, the number of ones in row r and between columns c1 and c2 (inclusive) is:

    hor_ones[r][c2+1] - hor_ones[r][c1]

    EDIT

    Here’s my solution in Java, maybe it is easier for you to understand and implement in 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array right now that looks like this: function art_appreciation_feeds() {
I have a multi-dimensional array that I am trying to feed into difflib.get_close_matches() .
I have an multi-dimensional array that I want to send to a PHP script
I have a 2-dimensional numpy array that looks like this: [[a b c] [d
I have a 2 dimensional array forming a table: [color][number][shape ] ------------------------- [black][10 ][square
I have a function which must return one dimensional associate array, like $user_info[$index]=value where
I have a two dimensional array that I need to rotate 90 degrees clockwise,
I have a 1-dimensional array that fills up a table of 40 random elements
So i have a 2 dimensional array that is used over several pages (session)
I have a one dimensional array in VBScript that I would like to run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.