<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Chaos at the Sky]]></provider_name><provider_url><![CDATA[https://chaosatthesky.wordpress.com]]></provider_url><author_name><![CDATA[chaotic_iak]]></author_name><author_url><![CDATA[https://chaosatthesky.wordpress.com/author/chaoticiak/]]></author_url><title><![CDATA[Greedy Queens Problem]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>In the famous Queens Problem, you need to put n chess queens on an n x n chessboard so no two queens attack each other. It has been proven that a solution exists for all n except n=2,3.</p>
<p>One of the methods (albeit an ineffective one) to solve it is by greedy algorithm, which does a depth-first search. In this case, a queen is placed on the first row, on the first column. The next queen is placed on the second row, starting from the first column. As it attacks a previous queen, it is moved to the second column, and again to the third column. Now as they don&#8217;t attack, a third queen is placed from the leftmost column rightward, and so on. When a queen cannot be placed, the previous queen is removed and placed to the next column. This guarantees a solution, as it checks every possible board as long as no conflict has been made.</p>
<p>For example, with n=4:<br />
First queen tries a1.<br />
Second queen tries a2. Conflict with a1.<br />
Second queen tries b2. Conflict with a1.<br />
Second queen tries c2.<br />
Third queen tries a3,b3,c3,d3 in order but conflicts each time. Second queen is retracted.<br />
Second queen tries d2.<br />
Third queen tries a3, conflict with a1.<br />
Third queen tries b3.<br />
Fourth queen tries a4,b4,c4,d4, all conflict. Third queen is retracted.<br />
Third queen tries c3,d3, all conflict. Third queen is retracted. Second queen is retracted (cannot go further).<br />
First queen tries b1.</p>
<p>This algorithm is run until the solution b1,d2,a3,c4 is found (the first solution found). In total, there are 26 queen tries.</p>
<p>For n=5, it can be verified that it takes 15 queen tries to arrive at a1,c2,e3,b4,d5.</p>
<p>Can you compute the number of queen tries for a given n effectively? In other words, is this problem in P? (Naively executing the algorithm gives a possible n^n steps, so it&#8217;s not polynomial. You need a better way.)</p>
<p>Extra Problem</p>
<p>A revised version of the algorithm doesn&#8217;t put a queen when it makes a conflict. So, for the n=4 example above, we have:</p>
<p>First queen on a1.<br />
Second queen can&#8217;t go on a2,b2. Second queen on c2.<br />
Third queen can&#8217;t go anywhere, second queen is retracted and go on d2.<br />
Third queen on b3.<br />
Fourth queen can&#8217;t go anywhere. Third queen retracted. Second queen retracted. First queen on b1.<br />
And so on.</p>
<p>It takes 8 queen tries now. For n=5, it takes 5 queen tries, or in other words the exact number required.</p>
<p>Can you compute this number effectively too?</p>
]]></html></oembed>