Note that there are some explanatory texts on larger screens.

plurals
  1. POKnight's tour algorithm using recursion
    primarykey
    data
    text
    <p>I have been writing code to solve Knight's tour problem. I wrote this code and i am little confused now. I read and analyze it all over again,several times and was not able to find an error that causes the problem. I will appreciate any help.</p> <pre><code>#include &lt;iostream&gt; const int N = 5; // size of table int moves_table[2][8] = { { 1, 2, 2, 1, -1, -2, -1, -1 }, { -2, -1, 1, 2, -1, -2, -2, -1 } }; //my moves table that i use to increment or decrement my x and y cordinates int THETABLE[N][N] = { 0 }; //all elements of table equal to zero bool iknight_tour(int x, int y, int r); // this function will return true if Knight is able to be on every place on chessmate, without being twice at the same place, starting from TABLE[x][y] bool if_move_correct(int x, int y); //checks if move can be done int main() { bool some_variable = iknight_tour(0, 0, 1); for (int i = 0; i &lt; N; i++) { for (int j = 0; j &lt; N; j++) { std::cout &lt;&lt; THETABLE[i][j] &lt;&lt; "\t"; } std::cout &lt;&lt; std::endl; } std::cout &lt;&lt; std::endl; std::cout &lt;&lt; some_variable; std::cout &lt;&lt; std::endl; return 0; } bool if_move_correct(int x, int y) { if (x &lt; N &amp;&amp; x &gt;= 0 &amp;&amp; y &lt; N &amp;&amp; y &gt;= 0 &amp;&amp; THETABLE[x][y] == 0) { return true; } else { return false; } } bool iknight_tour(int x, int y, int r) { THETABLE[x][y] = r; if (r == N*N) { return true; } else { for (int i = 0; i &lt; 8; i++) { { if ((if_move_correct(x + moves_table[0][i], y + moves_table[1][i]))==true) { if((iknight_tour(x + moves_table[0][i], y + moves_table[1][i], r + 1))==true) { return true; } } } } THETABLE[x][y] = 0; } return false; } </code></pre> <p>For example for i_kinghtour(0,0,1) i got :<br> 0 0 0 0 0<br> 0 0 0 0 0<br> 0 0 0 0 0<br> 0 0 0 0 0<br> 0 0 0 0 0 </p> <p>And i should get:<br> 1 20 17 12 3<br> 16 11 2 7 18<br> 21 24 19 4 13<br> 10 15 6 23 8<br> 25 22 9 14 5 </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.
 

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