Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit17b0a94

Browse files
author
Raghav Dua
authored
Merge pull requestalgorithm-visualizer#201 from archie94/knight-tour
Implemented Knight's Tour:Backtracking method
2 parents979dc91 +9b458d8 commit17b0a94

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
functionknightTour(x,y,moveNum){
2+
if(moveNum===N*N){
3+
returntrue;
4+
}
5+
6+
for(vari=0;i<8;i++){
7+
varnextX=x+X[i];
8+
varnextY=y+Y[i];
9+
10+
posTracer._notify(0,nextX)._wait();
11+
posTracer._notify(1,nextY)._wait();
12+
posTracer._denotify(0);
13+
posTracer._denotify(1);
14+
/*
15+
Check if knight is still in the board
16+
Check that knight does not visit an already visited square
17+
*/
18+
if(nextX>=0&&nextX<N&&nextY>=0&&nextY<N&&board[nextX][nextY]===-1){
19+
board[nextX][nextY]=moveNum;
20+
21+
logTracer._print('Move to '+nextX+','+nextY);
22+
boardTracer._notify(nextX,nextY,moveNum)._wait();
23+
boardTracer._denotify(nextX,nextY);
24+
boardTracer._select(nextX,nextY);
25+
26+
varnextMoveNum=moveNum+1;
27+
if(knightTour(nextX,nextY,nextMoveNum)===true){
28+
returntrue;
29+
}else{
30+
logTracer._print('No place to move from '+nextX+','+nextY+': Backtrack');
31+
board[nextX][nextY]=-1;// backtrack
32+
boardTracer._notify(nextX,nextY,-1)._wait();
33+
boardTracer._denotify(nextX,nextY);
34+
boardTracer._deselect(nextX,nextY);
35+
}
36+
}else{
37+
logTracer._print(nextX+','+nextY+' is not a valid move');
38+
}
39+
}
40+
returnfalse;
41+
}
42+
43+
board[0][0]=0;// start from this position
44+
pos[0]=0;
45+
pos[0]=0;
46+
47+
boardTracer._notify(0,0,0)._wait();
48+
posTracer._notify(0,0)._wait();
49+
posTracer._notify(1,0)._wait();
50+
boardTracer._denotify(0,0);
51+
boardTracer._denotify(0,0);
52+
posTracer._denotify(0);
53+
posTracer._denotify(1);
54+
55+
if(knightTour(0,0,1)===false){
56+
logTracer._print('Solution does not exist');
57+
}else{
58+
logTracer._print('Solution found');
59+
}
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+
varN=3;
7+
varboard=newArray(N);
8+
for(vari=board.length-1;i>=0;i--){
9+
board[i]=newArray(N);
10+
}
11+
12+
for(vari=board.length-1;i>=0;i--){
13+
for(varj=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+
varX=[2,1,-1,-2,-2,-1,1,2];
22+
varY=[1,2,2,1,-1,-2,-2,-1];
23+
24+
varpos=newArray(2);
25+
pos[0]=pos[1]=-1;
26+
27+
varboardTracer=newArray2DTracer('Board')._setData(board);
28+
varposTracer=newArray1DTracer('Knight Position')._setData(pos);
29+
varlogTracer=newLogTracer('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
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"backtracking": {
33
"list": {
4+
"knight's_tour":"Knight’s tour problem",
45
"n_queens":"N Queens Problem"
56
},
67
"name":"Backtracking"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp