Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is quite simple. If you have <code>n</code> squares, you can count the rectangles in <code>O(n)</code>.</p> <p>Assumptions:</p> <ul> <li>The borders of each valid rectangle don't share any cells with a non-valid path.</li> <li>If one rectangle is inside another, you would be happy finding them</li> </ul> <p>You would need extra memory as big as the input. Let's call this <code>visited</code> and initialize with 0.</p> <p>Let's first construct a helper function:</p> <pre><code>is_rectangle(square s) from s, go right while you see 1s and visited is 0 mark them as 1 in visited if less than 3 steps return 0 then, go down while you see 1s and visited is 0 mark them as 1 in visited if less than 3 steps return 0 then, go left while you see 1s and visited is 0 mark them as 1 in visited if less than 3 steps return 0 then, go up while you see 1s and visited is 0 mark them as 1 in visited if then one above is not s return 0 return 1 </code></pre> <p>This function basically traces the 1s in the directions of right-down-left-up and checks if the conditions are met (length at least 3 and reaching the starting position). It also marks the visited squares.</p> <p>The important thing to notice about this function, is that it works correctly only if the initial square is the top-left corner.</p> <p>Now, the solution to the problem is easy:</p> <pre><code>num_rectangles = 0 initialize visited to 0 (rows x columns) for r in rows for c in columns if visitied[r][c] or image[r][c] == 0 continue num_rectangles += is_rectangle(&lt;r,c&gt;) return num_rectangles </code></pre> <p>Here is how the algorithm executes:</p> <p><img src="https://i.stack.imgur.com/At6yQ.png" alt="enter image description here"><br> 1. Failed (and marked) part of a bad rectangle</p> <p><img src="https://i.stack.imgur.com/j4kXd.png" alt="enter image description here"><br> 2. Found (and marked) a rectangle.</p> <p><img src="https://i.stack.imgur.com/VYj4K.png" alt="enter image description here"><br> 3. Failed on a single square (of a vertical line)</p> <p><img src="https://i.stack.imgur.com/PwfS4.png" alt="enter image description here"><br> 4. Failed on a single square (of a vertical line)</p> <p><img src="https://i.stack.imgur.com/daWVu.png" alt="enter image description here"><br> 5. Failed on a single square (of a vertical line)</p> <p><img src="https://i.stack.imgur.com/esXv4.png" alt="enter image description here"><br> 6. After many similar steps, found the next rectangle.</p> <p><img src="https://i.stack.imgur.com/HTopH.png" alt="enter image description here"><br> 7. And the next rectangle</p> <p><img src="https://i.stack.imgur.com/QAcgR.png" alt="enter image description here"><br> 8. Algorithm end</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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