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
|
#include"header1.h" // inside just regular library and prototype
using namespace std;
void display(int *Knight, int n)
{
for (int i = 0; i < n; i++)
cout << i+1 << "," << Knight[i]+11 << endl; // I try to make the result look like an int
//that the first number is a row and the second is colunm
}
bool available(int row, int col, int n) // Check if the move is in the chessboard
{
if (row > n || col > n || row < 0 || col < 0)
return false;
return true;
}
void KnightTour(int *Knight,int n,int k,bool* row,bool* col,bool &one,int x,int y,int xMove[],int yMove[])
{
if (k >= n*n) // If complete
{
display(Knight,n*n);
one = false; // I want only one result so I put a bool to prevent other result
}
else
for (int i = 0; i < 8; ++i) // I check all possible 8 move of a knight
if (available(x+xMove[i],y+yMove[i],n) && (row[x+xMove[i]] && col[y+yMove[i]]) && one)
{
Knight[k] = (x+xMove[i])*10 + y+yMove[i]; //// I try to make the result look like an int
//that the first number is a row and the second is colunm
row[x+xMove[i]] = false; // I mark this row and col is used,
col[y+yMove[i]] = false; // If both are marked, The knight cannot come to that cell
KnightTour(Knight,n,k+1,row,col,one,x,y,xMove,yMove); // Recursive
row[x+xMove[i]] = true; // Unmarking
col[y+yMove[i]] = true;
}
}
void solve(int N, int x, int y)
{
int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int *n = new int;
*n = N;
bool *row = new bool[*n];
bool *col = new bool[*n];
for (int i = 0; i < *n; ++i)
{
row[i] = true;
col[i] = true;
}
int* Knight = new int[(*n)*(*n)];
bool one = true;
KnightTour(Knight,N,0,row,col,one,x-1,y-1,xMove,yMove);
}
|