Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think a greedy algorithm suffices, with one step per coin.</p> <p>Every move flips a rectangular subset of the board. Some coins are included in more subsets than others: the coin at (0,0) upper-left is in every subset, and the coin at lower-right is in only one subset, namely the one which includes every coin.</p> <p>So, choosing the first move is obvious: flip every coin if the lower-right corner must be flipped. Eliminate that possible move.</p> <p>Now, the lower-right coin's immediate neighbors, left and above, can only potentially be flipped by a single remaining move. So, if that move must be performed, do it. The order of evaluation of the neighbors doesn't matter, since they aren't really alternatives to each other. However, a raster pattern should suffice.</p> <p>Repeat until finished.</p> <p>Here is a C++ program:</p> <pre><code>#include &lt;iostream&gt; #include &lt;valarray&gt; #include &lt;cstdlib&gt; #include &lt;ctime&gt; using namespace std; void print_board( valarray&lt;bool&gt; const &amp;board, size_t cols ) { for ( size_t i = 0; i &lt; board.size(); ++ i ) { cout &lt;&lt; board[i] &lt;&lt; " "; if ( i % cols == cols-1 ) cout &lt;&lt; endl; } cout &lt;&lt; endl; } int main() { srand( time(NULL) ); int const rows = 5, cols = 5; valarray&lt;bool&gt; board( false, rows * cols ); for ( size_t i = 0; i &lt; board.size(); ++ i ) board[i] = rand() % 2; print_board( board, cols ); int taken_moves = 0; for ( size_t i = board.size(); i &gt; 0; ) { if ( ! board[ -- i ] ) continue; size_t sizes[] = { i%cols +1, i/cols +1 }, strides[] = { 1, cols }; gslice cur_move( 0, valarray&lt;size_t&gt;( sizes, 2 ), valarray&lt;size_t&gt;( strides, 2 ) ); board[ cur_move ] ^= valarray&lt;bool&gt;( true, sizes[0] * sizes[1] ); cout &lt;&lt; sizes[1] &lt;&lt; ", " &lt;&lt; sizes[0] &lt;&lt; endl; print_board( board, cols ); ++ taken_moves; } cout &lt;&lt; taken_moves &lt;&lt; endl; } </code></pre>
 

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