TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
knight_tour.cpp File Reference

Knight's tour algorithm More...

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

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.
 

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

Definition in file knight_tour.cpp.

Function Documentation

◆ issafe()

template<size_t V>
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

Template Parameters
Vnumber of vertices in array
Parameters
xcurrent index in rows
ycurrent index in columns
solmatrix where numbers are saved
Returns
true if ....
false if ....

Definition at line 40 of file knight_tour.cpp.

40 {
41 return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
42}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 88 of file knight_tour.cpp.

88 {
89 const int n = 8;
90 std::array<std::array<int, n>, n> sol = {0};
91
92 int i = 0, j = 0;
93 for (i = 0; i < n; i++) {
94 for (j = 0; j < n; j++) {
95 sol[i][j] = -1;
96 }
97 }
98
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};
101
102 sol[0][0] = 0;
103
104 bool flag = backtracking::knight_tour::solve<n>(0, 0, 1, sol, xmov, ymov);
105 if (flag == false) {
106 std::cout << "Error: Solution does not exist\n";
107 } else {
108 for (i = 0; i < n; i++) {
109 for (j = 0; j < n; j++) {
110 std::cout << sol[i][j] << " ";
111 }
112 std::cout << "\n";
113 }
114 }
115 return 0;
116}

◆ solve()

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 )

Knight's tour algorithm

Template Parameters
Vnumber of vertices in array
Parameters
xcurrent index in rows
ycurrent index in columns
movmovement to be done
solmatrix where numbers are saved
xmovnext move of knight (x coordinate)
ymovnext move of knight (y coordinate)
Returns
true if solution exists
false if solution does not exist

Definition at line 57 of file knight_tour.cpp.

58 {
59 int k = 0, xnext = 0, ynext = 0;
60
61 if (mov == V * V) {
62 return true;
63 }
64
65 for (k = 0; k < V; k++) {
66 xnext = x + xmov[k];
67 ynext = y + ymov[k];
68
69 if (issafe<V>(xnext, ynext, sol)) {
70 sol[xnext][ynext] = mov;
71
72 if (solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
73 return true;
74 } else {
75 sol[xnext][ynext] = -1;
76 }
77 }
78 }
79 return false;
80}
double k(double x)
Another test function.
void mov(tower *From, tower *To)