Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Clearly, you need to check horizontal, vertical, and diagonal lines.</p> <p>Let us assume that the board is laid out with n always being the bigger number if the two are not equal, and that it is laying on its side" (lego style, not skyscraper style). So it is <code>n</code> across and <code>m</code> tall.</p> <p>The horizontal lines will be <code>n * (m - l)</code> in number.</p> <p>The vertical lines will be <code>m * (n - l)</code> in number.</p> <p>The diagonal lines will be <code>(m - l) * (n - l)</code>, or <code>m*n - l*m - l*n + l*l</code></p> <p>If we assume <code>n &gt;= m &gt; l</code> then we can safely say that it is within <code>O(n^2)</code>, as we would expect for a two dimensional board.</p> <p>We know that <code>l &gt; n &gt;= m</code> will have no results. If <code>n = m = l</code> we have a constant number (<code>2*n + 2</code>). If <code>n = l &gt; m</code> we are left with an even better case, as we don't have to check the diagonals or the verticals, and you only have to check <code>m</code> lines. If <code>n &gt; l &gt; m</code>, then we can again exclude the verticals, but must consider the diagonals. In any event, it will be less than doing the diagonals, verticals, and horizontals.</p> <p>There is an optimization that can be made, however, based on the <a href="https://stackoverflow.com/users/383124/phant0m">phant0m's</a> comment. It requires that you know what the last move made was.</p> <p>You can safely assume that if a move was made, it was made at a time that there was no winner on the board. Therefore, if there is a win condition on the board after the move, it must have been formed as a result of the most recent move. Therefore, the worst case scenario given this information is that the winning line is formed with the most recent move on the end. You would therefore need to check the 4 line segments that extend <code>l - 1</code> in each direction (horizontal, vertical, forward diagonal, and backward diagonal). This is a total of <code>4 * (2*l - 1)</code>, putting it safely in <code>O(l)</code>. Considering you only need to store one additional coordinate (most recent move) this is a most wise optimization to make.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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