Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd do something like this (pseudocode):</p> <pre><code>// your 2d-array / matrix (N is the number of lines, M the number of columns) m[N][M] = ... // x coord of top left object containing 1 baseM = -1 baseFound = false // expected width of rectangle width = 0 widthLocked = false // this is set to true after we started recognizing a rectangle and encounter // a row where the object expected to be 1 in order to extend the rectangle // is 0. heightExceeded = false // loop over matrix for i = 1 to N: // lines // at the beginning of a line, if we already found a base, lock the width // (it cannot be larger than the number of 1s in the row of the base) if baseFound: widthLocked = true for j = 1 to M: // columns if m[i][j] == 1: if not baseFound: baseM = j, baseFound = true width = 1 else: if j &lt; baseM: // not in rectangle in negative x direction return false if heightExceeded: // not in rectangle in y direction return false if widthLocked: // not in rectangle in positive x direction if j - baseM &gt;= width: return false else: width = j - baseM elseif baseFound: if widthLocked: // check if we left the rectangle and memorize it if j == baseM: heightExceeded = true if not heightExceeded: // check if object in rectangle is 0 if j &gt; baseM &amp;&amp; j &lt; baseM + width: return false if baseFound: return true else: // what is the expected result if no rectangle has been found? return ? </code></pre> <p>Runs in O(n). Beware of bugs.</p> <p>Note: Most programming languages have 0-based arrays, so you may need to loop <code>i</code> from <code>0</code> to <code>N - 1</code>, same for <code>j</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload