The knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square once. If the knight is able to visit every square and ends up on the beginning square as its last move, this is a complete knight tour.
My program written in Java and implements Stacks and Backtracking in order to find a solution for boards between 3x3 and 8x8.
Download both KnightTour.java
and KnightBoard.java
. The knight tour can be run by calling $ java KnightTour n
where n
is an n x n
size board.
$ java KnightTour 5
Possible Knight Tour with max #squares visited in this (5x5) board:
+--+--+--+--+--+
| 1|10| 5|16|25|
+--+--+--+--+--+
| 4|17| 2|11| 6|
+--+--+--+--+--+
| 9|20|13|24|15|
+--+--+--+--+--+
|18| 3|22| 7|12|
+--+--+--+--+--+
|21| 8|19|14|23|
+--+--+--+--+--+
(Time to find this solution = 70.3757 milliseconds)
KnightBoard
: Chessboard class is a 2D array that hasn
size, keeps track of the number of moves inmoveCount
, and current position of the knight incurrentX
andcurrentY
. There are also some helper methods such asprintChessBoard
,copyBoard
, andmove
(checks if a knights move to a specific position is valid). There are also setters and getters to modifymoveCount
,currentX
, andcurrentY
positions.KnightTour
: Implements and prints a valid knight tour. This does not print all valid knight tours as the valid number of tours for a 6x6 board is 6,637,920 and the it only gets exponentially larger. The program essentially has all 8 moves of a knight listed out in two separate arrayspossibleRow
andpossibleCol
. It only adds valid moves from the current position to the stack. This for every single space that the knight can occupy until a board is found wherecurrentBoard.getMoveCount() == n * n
or ifn == 3 || n == 4
there is no solution where every space is visited so a solution with all but one space visited is valid.
Size | Time in ms |
---|---|
3x3 | 0.771277 |
4x4 | 10.917376 |
5x5 | 55.889333 |
6x6 | 236.791035 |
7x7 | 90.314049 |
8x8 | 94947.098818 |
$ java KnightTour 3
Stack: Round 1
+--+--+--+
| 1| | |
+--+--+--+
| | | |
+--+--+--+
| | | |
+--+--+--+
Stack: Round 2 (two more possible moves are added to the stack)
+--+--+--+ +--+--+--+
| 1| | | | 1| | |
+--+--+--+ +--+--+--+
| | | 2| | | | |
+--+--+--+ +--+--+--+
| | | | | | 2| |
+--+--+--+ +--+--+--+
Stack: Round 3 (explores all possible moves from the last board added to the stack)
+--+--+--+
| 1| | |
+--+--+--+
| | | 2|
+--+--+--+
| 3| | |
+--+--+--+
Stack: Round 4
+--+--+--+
| 1| 4| |
+--+--+--+
| | | 2|
+--+--+--+
| 3| | |
+--+--+--+
Stack: Round 5
...
Stack: Round 6
...
Stack: Round 7
...
Stack: Round 8
+--+--+--+
| 1| 4| 7|
+--+--+--+
| 6| | 2|
+--+--+--+
| 3| 8| 5|
+--+--+--+
knight-board's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.