TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Knight's tour algorithm More...
#include <array>
#include <iostream>
Go to the source code of this file.
Namespaces | |
namespace | backtracking |
for vector container | |
namespace | knight_tour |
Functions for the Knight's tour algorithm. | |
Functions | |
template<size_t V> | |
bool | backtracking::knight_tour::issafe (int x, int y, const std::array< std::array< int, V >, V > &sol) |
template<size_t V> | |
bool | backtracking::knight_tour::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 () |
Main function. | |
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.
Definition in file knight_tour.cpp.
bool backtracking::knight_tour::issafe | ( | int | x, |
int | y, | ||
const std::array< std::array< int, V >, V > & | sol ) |
A utility function to check if i,j are valid indexes for N*N chessboard
V | number of vertices in array |
x | current index in rows |
y | current index in columns |
sol | matrix where numbers are saved |
true
if .... false
if .... Definition at line 40 of file knight_tour.cpp.
int main | ( | void | ) |
Main function.
Definition at line 88 of file knight_tour.cpp.
bool backtracking::knight_tour::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 ) |
Knight's tour algorithm
V | number of vertices in array |
x | current index in rows |
y | current index in columns |
mov | movement to be done |
sol | matrix where numbers are saved |
xmov | next move of knight (x coordinate) |
ymov | next move of knight (y coordinate) |
true
if solution exists false
if solution does not exist Definition at line 57 of file knight_tour.cpp.