From 9b458d82aed924d3f1bbe22093af16545bd1483d Mon Sep 17 00:00:00 2001 From: archie94 Date: Sun, 3 Jul 2016 09:34:45 +0530 Subject: [PATCH] Implemented Knight's Tour:Backtracking method --- .../backtracking/knight's_tour/basic/code.js | 59 +++++++++++++++++++ .../backtracking/knight's_tour/basic/data.js | 29 +++++++++ .../backtracking/knight's_tour/desc.json | 13 ++++ algorithm/category.json | 1 + 4 files changed, 102 insertions(+) create mode 100644 algorithm/backtracking/knight's_tour/basic/code.js create mode 100644 algorithm/backtracking/knight's_tour/basic/data.js create mode 100644 algorithm/backtracking/knight's_tour/desc.json diff --git a/algorithm/backtracking/knight's_tour/basic/code.js b/algorithm/backtracking/knight's_tour/basic/code.js new file mode 100644 index 00000000..7b073a8b --- /dev/null +++ b/algorithm/backtracking/knight's_tour/basic/code.js @@ -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=0 && nextY3 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'); \ No newline at end of file diff --git a/algorithm/backtracking/knight's_tour/desc.json b/algorithm/backtracking/knight's_tour/desc.json new file mode 100644 index 00000000..2b4af117 --- /dev/null +++ b/algorithm/backtracking/knight's_tour/desc.json @@ -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(8N2)", + "space": "Worst O(N2)" + }, + "References": [ + "Wikipedia" + ], + "files": { + "basic": "Solving the Knight’s tour problem using Backtracking & Recursion" + } +} diff --git a/algorithm/category.json b/algorithm/category.json index 980c8470..c1d04c63 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -1,6 +1,7 @@ { "backtracking": { "list": { + "knight's_tour": "Knight’s tour problem", "n_queens": "N Queens Problem" }, "name": "Backtracking" 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