Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a bit brute-force-ish but would probably work.</p> <p>You might try to conceptually break the board into layers for instance:</p> <pre><code>------------------------- 0 | 1 | 1 | 0 | 1 | 1 | 0 1 | 0 |-1 | 1 | 0 | 0 | 1 1 | 1 | 1 | 1 | 1 | 1 | 1 ------------------------- </code></pre> <p>Looking at just the lowest layer. Assuming -1 is the bottom, the board would look like this:</p> <pre><code>------------------------- 0 | 0 | 0 | 0 | 0 | 0 | 0 0 | 0 |-1 | 0 | 0 | 0 | 0 0 | 0 | 0 | 0 | 0 | 0 | 0 ------------------------- </code></pre> <p>For each square, determine if there exists a square to the left, right, top and bottom that all have a greater value. In this case we count 1.</p> <p>Then move to the next layer, filling in the "holes" in the last layer.</p> <pre><code>------------------------- 0 | 1 | 1 | 0 | 1 | 1 | 0 1 | 0 | 0 | 1 | 0 | 0 | 1 1 | 1 | 1 | 1 | 1 | 1 | 1 ------------------------- </code></pre> <p>Rinse, repeat. In this layer we count 4 giving us a total of 5.</p> <pre><code>------------------------- 1 | 1 | 1 | 1 | 1 | 1 | 1 1 | 1 | 1 | 1 | 1 | 1 | 1 1 | 1 | 1 | 1 | 1 | 1 | 1 ------------------------- </code></pre> <p>Top layer obviously has none and we're done.</p> <p>In pseudo code:</p> <pre><code>for each layer l in layers for each square in l if there exists a square left, right, top and bottom with higher value count the square. </code></pre> <p><strong>Edit</strong></p> <p>Of course since there is something seriously wrong with me, when I woke up this morning the first thing I thought of was this problem and immediately broke my solution.</p> <p>Let's make one change to the example:</p> <pre><code>------------------------- 0 | 1 | 1 | 0 | 1 | 1 | 0 1 | 0 |-1 | 1 | 0 | 0 | 1 1 | 1 | 1 | 1 | 1 | 0 | 1 ------------------------- ^ </code></pre> <p>Open a hole to the outside. With the current algorithm we would get a solution of 4 which is obviously wrong.</p> <p>To fix this we need to implement a back tracking algorithm.</p> <p>Instead of looking anywhere to the left, right, top and bottom for a higher value, we check just the immediately adjacent squares. If any are at the same height we need to visit that square as well and perform the check again. If we find our way to the outside then the original square (and subsequently all the visited squares) fail. If we hit a dead end, then all the visited squares can be counted.</p> <p>With this modification we would get the correct result of 3.</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.
 

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