Knight's Tour

I have to make code for knight's tour. Knight chess can visit only one box only once. Starting position is 0,0. This loop doesnt make any jump. I know I must make one more if when knight cant make new jump. But I want to know what is incorretly with main loop and board.Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

using namespace std;

int main()
{
     const int n=8;

     int board[n][n]={};


     int horizon[8]={1,2,2,1,-1,-2,-2,-1};  // 8 possible moves 
     int vertical[8]={2,-1,1,-2,2,1,-1,-2};

     int current_horizon=0; //starting positon
     int current_vertical=0;
     
     int nr_jump=1;

     board[0][0]=1; //start pole is visited


     for(int j=0;j<64;j++)
     {
         for (int i=0;i<=7;i++)
            {
                current_horizon+=horizon[i]; 
                current_vertical+=vertical[i];

                    if((current_vertical>=0)&&(current_vertical<=n-1)&&(current_horizon>=0)&&(current_horizon<=n-1)&&
                       (board[current_horizon][current_vertical]==0))
                         {
                              nr_jump+=nr_jump;
                              board[current_vertical][current_horizon]=nr_jump;

                         }
                    else
                        {
                            current_horizon=current_horizon-horizon[i];
                            current_vertical=current_vertical-vertical[i]; 

                        }
            }   // There sholud be if to back knight move when it cant make jump
     }

        if(nr_jump==64)
          {
                  board[8][8];
                    for(int i=0;i<8;i++)
                        {
                            for(int j=0;j<8;j++)
                                cout <<board [i][j] << " ";
                                cout << endl;
                        }
          }





       return 0;


}
I will get back to you when ready and I will give you some hints
I found a way to display board...
on line 46 change it to
 
 if(nr_jump=64)
That's right. But this loop ://
You need some way to backtrack when you can no longer continue along a given path. This implies that you need to keep track of the order in which you've visited board positions.


I found a way to display board...
on line 46 change it to
if(nr_jump=64)
This would have the same practical effect as removing the if altogether. Of course, the board inside the if body has no relation to the board that was manipulated elsewhere, and is, in fact, filled with junk.

Topic archived. No new replies allowed.