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
|
#include <iostream>
using namespace std;
int xs[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int ys[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int b[12][12];
bool knighttour(int x, int y, int s)
{
if (s == 64){
return true;
}
for (int i=0; i<8; i++){
if (b[x+xs[i]][y+ys[i]] == 0){
b[x+xs[i]][y+ys[i]] = s;
if (knighttour(x+xs[i], y+ys[i], s+1)){
return true;
}
else{
b[x+xs[i]][y+ys[i]] = 0;
}
}
}
return false;
}
int main()
{
for (int i=0; i<12; i++){
for (int j=0; j<12; j++){
if (i>1 && i<10 && j>1 && j<10){b[j][i] = 0;}
else {b[j][i] = 1;}
}
}
int x, y;
cout << "Initial Coordinate (x y): ";
cin >> x >> y;
x += 1; y += 1;
b[x][y] = 1;
knighttour(x, y, 2);
for (int i=2; i<10; i++){
for (int j=2; j<10; j++){
if (b[j][i]<10){cout << " " << b[j][i] << " ";}
else {cout << b[j][i] << " ";}
}
cout << endl;
}
return 0;
}
|