knight_tour.cpp File Reference

Knight's tour algorithm More...

`#include <iostream>`
`#include <array>`
Include dependency graph for knight_tour.cpp:

## Namespaces

| backtracking
| Backtracking algorithms.

## Functions

template<size_t V>
bool | backtracking::issafe (int x, int y, const std::array< std::array< int, V >, V > &sol)
template<size_t V>
bool | backtracking::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)
int | main ()

## Detailed Description

Knight's tour algorithm

A knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. If the knight ends on a square that is one knight's move from the beginning square (so that it could tour the board again immediately, following the same path, the tour is closed; otherwise, it is open.

Author
  Nikhil Arora
  David Leal

## Function Documentation

## main()

int main | ( | void |  | ) |

Main function

{
const int n = 8;
std::array <std::array <int, n>, n> sol = { 0 };
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) { sol[i][j] = -1; }
}
std::array <int, n> xmov = { 2, 1, -1, -2, -2, -1, 1, 2 };
std::array <int, n> ymov = { 1, 2, 2, 1, -1, -2, -2, -1 };
sol[0][0] = 0;
bool flag = backtracking::solve<n>(0, 0, 1, sol, xmov, ymov);
if (flag == false) {
std::cout << "Error: Solution does not exist\n";
}
else {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) { std::cout << sol[i][j] << " "; }
std::cout << "\n";
}
}
return 0;
}
std::array
std::cout