1+ // import visualization libraries {
12const { Tracer, Array1DTracer, Array2DTracer, LogTracer, Layout, VerticalLayout } = require ( 'algorithm-visualizer' ) ;
3+ // }
24
35/*
46For N>3 the time taken by this algorithm is sufficiently high
@@ -26,13 +28,15 @@ const Y = [1, 2, 2, 1, -1, -2, -2, -1];
2628const pos = new Array ( 2 ) ;
2729pos [ 0 ] = pos [ 1 ] = - 1 ;
2830
31+ // define tracer variables {
2932const boardTracer = new Array2DTracer ( 'Board' ) ;
3033const posTracer = new Array1DTracer ( 'Knight Position' ) ;
3134const logTracer = new LogTracer ( 'Console' ) ;
3235boardTracer . set ( board ) ;
3336posTracer . set ( pos ) ;
3437Layout . setRoot ( new VerticalLayout ( [ boardTracer , posTracer , logTracer ] ) ) ;
3538Tracer . delay ( ) ;
39+ // }
3640
3741function knightTour ( x , y , moveNum ) {
3842 if ( moveNum === N * N ) {
@@ -43,37 +47,48 @@ function knightTour(x, y, moveNum) {
4347 const nextX = x + X [ i ] ;
4448 const nextY = y + Y [ i ] ;
4549
50+ // visualize {
4651 posTracer . patch ( 0 , nextX ) ;
4752 Tracer . delay ( ) ;
4853 posTracer . patch ( 1 , nextY ) ;
4954 Tracer . delay ( ) ;
5055 posTracer . depatch ( 0 ) ;
5156 posTracer . depatch ( 1 ) ;
57+ // }
5258 /*
5359 Check if knight is still in the board
5460 Check that knight does not visit an already visited square
5561 */
5662 if ( nextX >= 0 && nextX < N && nextY >= 0 && nextY < N && board [ nextX ] [ nextY ] === - 1 ) {
5763 board [ nextX ] [ nextY ] = moveNum ;
5864
65+ // visualize {
5966 logTracer . println ( `Move to ${ nextX } ,${ nextY } ` ) ;
6067 boardTracer . patch ( nextX , nextY , moveNum ) ;
6168 Tracer . delay ( ) ;
6269 boardTracer . depatch ( nextX , nextY ) ;
6370 boardTracer . select ( nextX , nextY ) ;
71+ // }
6472
6573 const nextMoveNum = moveNum + 1 ;
6674 if ( knightTour ( nextX , nextY , nextMoveNum ) === true ) {
6775 return true ;
6876 }
77+
78+ // logger {
6979 logTracer . println ( `No place to move from ${ nextX } ,${ nextY } : Backtrack` ) ;
80+ // }
7081 board [ nextX ] [ nextY ] = - 1 ; // backtrack
82+ // visualize {
7183 boardTracer . patch ( nextX , nextY , - 1 ) ;
7284 Tracer . delay ( ) ;
7385 boardTracer . depatch ( nextX , nextY ) ;
7486 boardTracer . deselect ( nextX , nextY ) ;
87+ // }
7588 } else {
89+ // logger {
7690 logTracer . println ( `${ nextX } ,${ nextY } is not a valid move` ) ;
91+ // }
7792 }
7893 }
7994 return false ;
@@ -83,6 +98,7 @@ board[0][0] = 0; // start from this position
8398pos [ 0 ] = 0 ;
8499pos [ 0 ] = 0 ;
85100
101+ // visualize {
86102boardTracer . patch ( 0 , 0 , 0 ) ;
87103Tracer . delay ( ) ;
88104posTracer . patch ( 0 , 0 ) ;
@@ -93,9 +109,12 @@ boardTracer.depatch(0, 0);
93109boardTracer . depatch ( 0 , 0 ) ;
94110posTracer . depatch ( 0 ) ;
95111posTracer . depatch ( 1 ) ;
112+ // }
96113
114+ // logger {
97115if ( knightTour ( 0 , 0 , 1 ) === false ) {
98116 logTracer . println ( 'Solution does not exist' ) ;
99117} else {
100118 logTracer . println ( 'Solution found' ) ;
101119}
120+ // }
0 commit comments