Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the longest sequence on a board?
    text
    copied!<p>I need a function that does the following:</p> <p>As input, it takes a n by n board, on which each spot is either empty, has a black stone or a white stone, and a position.</p> <p>As output, it returns the length of the longest sequence from the given position. If the position is empty, it returns 0.</p> <p>For example, if this board was inputted.</p> <pre><code>BUUUU WWWB UUUUU </code></pre> <p>where B is a black stone, W is a white stone and U is an empty spot, and the position inputted was (1,0), the output would 3.</p> <p>Or if this board was inputted:</p> <pre><code>BUUU WUUU BUUU BUUU </code></pre> <p>and the position was 0,3 the output would be zero, since the position is empty. If the position was 3,0 the output would be 2 because of the black column.</p> <p>Sequences can be horizontal, vertical or diagonal.</p> <p>This is what I have so far:</p> <p>If I'm given a position, I loop up, down, both sides, and both diagonals. I keep lopping until I find the sequence breaks, and then return the longest sequence. For example, if this is the board:</p> <pre><code>WUU WWU WUU </code></pre> <p>and the position was 1,0. I would loop sideways and find that the longest horizontal sequence is 1, loop diagonally, find the longest sequence is 1 and then loop vertically and find the longest sequence is 3, and therefore return 3.</p> <p>How can I do this faster? This function needs to be called around 10 million times in one second. My current function can execute around 8 million times per second.</p>
 

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