TheAlgorithms-C-Plus-Plus/backtracking/knight_tour.cpp
David Leal 25b39a34fa
[fix/docs]: Update backtracking folder (#916)
* [fix/docs]: Update backtracking/graph_coloring.cpp

* Add CMakeLists.txt in backtracking folder

* Add backtracking to CMakeLists.txt

* fix: Fix build issues

* docs: Various documentation fixes

* fix: minimax.cpp issues

* fix: sudoku_solve.cpp fixes

* formatting source-code for 8ffbbb35ce

* make he code neat and clean without global variables

* fix 2 stars in comment

* fix MSVC errors by forcing template parameter in function calls

Note: This is identical to passing it as a function parameter, and may not be helpful

* Update minimax.cpp

* docs: minimax.cpp improvements

* docs: Add Wikipedia link in minimax.cpp

* fix: minimax.cpp vector fix

* docs: fix Wikipedia link in minimax.cpp

* docs: fix return statement in minimax.cpp

* fix: sudoku_solve.cpp fixes

* fix: more sudoku_solve.cpp fixes

* fix: sudoku_solve.cpp fixes

* fix: sudoku_solve.cpp

* formatting source-code for 13b5b9b829

* docs: update graph_coloring.cpp description

* fix: use array instead of vector (minimax.cpp)

* feat: add namespace (minimax.cpp)

* docs: update namespace description (graph_coloring.cpp)

* fix: graph_coloring.cpp

* fix: sudoku_solve.cpp fixes

* fix: graph_coloring.cpp

* fix: minimax.cpp

* fix: more sudoku_solve.cpp fixes

* fix: more graph_coloring.cpp fixes

* fix: graph_coloring.cpp fixes

* fix: sudoku_solve.cpp fixes

* fix: minimax.cpp

* fix: sudoku_solve.cpp fixes

* fix: too few template arguments (std::array)

* fix: too few template arguments (std::array, minimax.cpp)

* fix: narrowing conversion from double to int (minimax.cpp)

* fix: excess elements in struct initializer (graph_coloring.cpp)

* fix: no matching function (graph_coloring.cpp)

* fix: graph_coloring.cpp issues/errors

* fix: knight_tour.cpp issues/errors

* fix: sudoku_solve.cpp issues/errors

* [fix/docs]: Various fixes in graph_coloring.cpp

* fix: More graph_coloring.cpp fixes

* docs: Add initial comment block (sudoku_solve.cpp)

* fix: Add return statement (knight_tour.cpp)

* fix: array fixes (graph_coloring.cpp)

* docs: documentation improvements (sudoku_solve.cpp)

* docs: documentation improvements (knight_tour.cpp)

* docs: documentation improvements (sudoku_solve.cpp)

* docs: documentation improvements (graph_coloring.cpp)

* docs: Documentation improvements (graph_coloring.cpp)

Thanks, @kvedala!

* docs: Documentation improvements (sudoku_solve.cpp)

* docs: Document function parameter (sudoku_solve.cpp)

* docs: Documentation improvements (knight_tour.cpp)

* docs: Add long description (graph_coloring.cpp)

* docs: Add long description (minimax.cpp)

* docs: Add long description (sudoku_solve.cpp)

* docs: Documentation improvements (knight_tour.cpp)

* docs: Documentation improvements (sudoku_solve.cpp)

* docs: Documentation improvements (minimax.cpp)

* docs: More documentation improvements (minimax.cpp)

* docs: Documentation improvements (sudoku_solve.cpp)

* fix: sudoku_solve.cpp improvements

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
2020-08-07 14:35:59 -04:00

106 lines
3.1 KiB
C++

/**
* @file
* @brief [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
*
* @details
* 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](https://github.com/nikhilarora068)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <iostream>
#include <array>
/**
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* A utility function to check if i,j are valid indexes for N*N chessboard
* @tparam V number of vertices in array
* @param x current index in rows
* @param y current index in columns
* @param sol matrix where numbers are saved
* @returns `true` if ....
* @returns `false` if ....
*/
template <size_t V>
bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) {
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
}
/**
* Knight's tour algorithm
* @tparam V number of vertices in array
* @param x current index in rows
* @param y current index in columns
* @param mov movement to be done
* @param sol matrix where numbers are saved
* @param xmov next move of knight (x coordinate)
* @param ymov next move of knight (y coordinate)
* @returns `true` if solution exists
* @returns `false` if solution does not exist
*/
template <size_t V>
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) {
int k, xnext, ynext;
if (mov == V * V) {
return true;
}
for (k = 0; k < V; k++) {
xnext = x + xmov[k];
ynext = y + ymov[k];
if (backtracking::issafe<V>(xnext, ynext, sol)) {
sol[xnext][ynext] = mov;
if (backtracking::solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
return true;
}
else {
sol[xnext][ynext] = -1;
}
}
}
return false;
}
} // namespace backtracking
/**
* Main function
*/
int main() {
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;
}