57bool solve(
int x,
int y,
int mov, std::array<std::array<int, V>, V> &sol,
58 const std::array<int, V> &xmov, std::array<int, V> &ymov) {
59 int k = 0, xnext = 0, ynext = 0;
65 for (k = 0; k < V; k++) {
69 if (issafe<V>(xnext, ynext, sol)) {
70 sol[xnext][ynext] =
mov;
72 if (solve<V>(xnext, ynext,
mov + 1, sol, xmov, ymov) ==
true) {
75 sol[xnext][ynext] = -1;
90 std::array<std::array<int, n>, n> sol = {0};
93 for (i = 0; i < n; i++) {
94 for (j = 0; j < n; j++) {
99 std::array<int, n> xmov = {2, 1, -1, -2, -2, -1, 1, 2};
100 std::array<int, n> ymov = {1, 2, 2, 1, -1, -2, -2, -1};
104 bool flag = backtracking::knight_tour::solve<n>(0, 0, 1, sol, xmov, ymov);
106 std::cout <<
"Error: Solution does not exist\n";
108 for (i = 0; i < n; i++) {
109 for (j = 0; j < n; j++) {
110 std::cout << sol[i][j] <<
" ";
bool solve(int x, int y, int mov, std::array< std::array< int, V >, V > &sol, const std::array< int, V > &xmov, std::array< int, V > &ymov)