Implemented Knight's Tour:Backtracking method · boro741/AlgorithmVisualizer@9b458d8 · GitHub
Skip to content

Commit 9b458d8

Browse files
committed
Implemented Knight's Tour:Backtracking method
1 parent 7c94540 commit 9b458d8

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
For N>3 the time taken by this algorithm is sufficiently high
3+
Also it is not possible to visualise for N>6 due to stack overflow
4+
caused by large number of recursive calls
5+
*/
6+
var N = 3;
7+
var board = new Array (N);
8+
for (var i = board.length - 1; i >= 0; i--) {
9+
board[i] = new Array (N);
10+
}
11+
12+
for (var i = board.length - 1; i >= 0; i--) {
13+
for (var j = board[i].length - 1; j >= 0; j--) {
14+
board[i][j] = -1;
15+
}
16+
}
17+
18+
/*
19+
Define the next move of the knight
20+
*/
21+
var X = [ 2, 1, -1, -2, -2, -1, 1, 2 ];
22+
var Y = [ 1, 2, 2, 1, -1, -2, -2, -1 ];
23+
24+
var pos = new Array (2);
25+
pos[0] = pos[1] = -1;
26+
27+
var boardTracer = new Array2DTracer ('Board')._setData (board);
28+
var posTracer = new Array1DTracer ('Knight Position')._setData (pos);
29+
var logTracer = new LogTracer ('Console');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Knight’s tour problem": "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.",
3+
"Complexity": {
4+
"time": "Worst O(8<sup>N<sup>2</sup></sup>)",
5+
"space": "Worst O(N<sup>2</sup>)"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Knight%27s_tour'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Solving the Knight’s tour problem using Backtracking & Recursion"
12+
}
13+
}

algorithm/category.json

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)