Skip to content

Implemented Knight's Tour:Backtracking method #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions algorithm/backtracking/knight's_tour/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function knightTour(x, y, moveNum) {
if (moveNum === N*N) {
return true;
}

for (var i = 0; i < 8; i++) {
var nextX = x + X[i];
var nextY = y + Y[i];

posTracer._notify ( 0, nextX)._wait ();
posTracer._notify ( 1, nextY)._wait ();
posTracer._denotify (0);
posTracer._denotify (1);
/*
Check if knight is still in the board
Check that knight does not visit an already visited square
*/
if (nextX>=0 && nextX<N && nextY>=0 && nextY<N && board[nextX][nextY]===-1) {
board[nextX][nextY] = moveNum;

logTracer._print ('Move to ' + nextX + ',' + nextY);
boardTracer._notify ( nextX, nextY, moveNum)._wait();
boardTracer._denotify( nextX, nextY);
boardTracer._select ( nextX, nextY);

var nextMoveNum = moveNum + 1;
if ( knightTour (nextX,nextY, nextMoveNum) === true) {
return true;
} else {
logTracer._print ('No place to move from ' + nextX + ',' +nextY + ': Backtrack');
board[nextX][nextY] = -1; // backtrack
boardTracer._notify ( nextX, nextY, -1)._wait();
boardTracer._denotify( nextX, nextY);
boardTracer._deselect( nextX, nextY);
}
} else {
logTracer._print (nextX + ',' + nextY + ' is not a valid move');
}
}
return false;
}

board[0][0] = 0; // start from this position
pos[0] = 0;
pos[0] = 0;

boardTracer._notify ( 0, 0, 0)._wait();
posTracer._notify ( 0, 0)._wait ();
posTracer._notify ( 1, 0)._wait ();
boardTracer._denotify( 0, 0);
boardTracer._denotify( 0, 0);
posTracer._denotify (0);
posTracer._denotify (1);

if (knightTour ( 0, 0, 1) === false ) {
logTracer._print ('Solution does not exist');
} else {
logTracer._print ('Solution found');
}
29 changes: 29 additions & 0 deletions algorithm/backtracking/knight's_tour/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
For N>3 the time taken by this algorithm is sufficiently high
Also it is not possible to visualise for N>6 due to stack overflow
caused by large number of recursive calls
*/
var N = 3;
var board = new Array (N);
for (var i = board.length - 1; i >= 0; i--) {
board[i] = new Array (N);
}

for (var i = board.length - 1; i >= 0; i--) {
for (var j = board[i].length - 1; j >= 0; j--) {
board[i][j] = -1;
}
}

/*
Define the next move of the knight
*/
var X = [ 2, 1, -1, -2, -2, -1, 1, 2 ];
var Y = [ 1, 2, 2, 1, -1, -2, -2, -1 ];

var pos = new Array (2);
pos[0] = pos[1] = -1;

var boardTracer = new Array2DTracer ('Board')._setData (board);
var posTracer = new Array1DTracer ('Knight Position')._setData (pos);
var logTracer = new LogTracer ('Console');
13 changes: 13 additions & 0 deletions algorithm/backtracking/knight's_tour/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"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.",
"Complexity": {
"time": "Worst O(8<sup>N<sup>2</sup></sup>)",
"space": "Worst O(N<sup>2</sup>)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Knight%27s_tour'>Wikipedia</a>"
],
"files": {
"basic": "Solving the Knight’s tour problem using Backtracking & Recursion"
}
}
1 change: 1 addition & 0 deletions algorithm/category.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"backtracking": {
"list": {
"knight's_tour": "Knight’s tour problem",
"n_queens": "N Queens Problem"
},
"name": "Backtracking"
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy