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;
}
|