Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knight's tour algorithm with adjacency-lists

I'm trying to solve the Knight's tour problem in Java. My goal is to calculate all possible tours of a horse on a chessboard with any dimension. What I have tried to use is an adjadency-lists data structure. The problem now, is that I know which squares are adjacent to a square, but I don't know what direction the adjacent square is in. How would I fix this?

like image 767
ShinyForce Avatar asked Jan 10 '16 22:01

ShinyForce


People also ask

Which approach is used for solving Knight's Tour problem?

The knight's tour problem is an instance of the more general Hamiltonian path problem in graph theory. The problem of finding a closed knight's tour is similarly an instance of the Hamiltonian cycle problem. Unlike the general Hamiltonian path problem, the knight's tour problem can be solved in linear time.

Is 4x4 Knights tour possible?

For example, on a 4x4 chessboard a knight's tour is also impossible. In fact, the 5 × 6 and the 3 x 10 chessboards are the smallest rectangular boards that have knight's tours.

Is it possible to move a knight on a chessboard such it every permissible move exactly once?

The answer is: NO, mission impossible. The proof is pretty simple: To complete a knight's tour, the knight must make 63 moves- no more, no less.

Who created the Knight's Tour?

Assigning a value to each square (determined by the knight's move order) and adding the numbers on each column and each row together always result in 260. William Beverley composed the first magic knight's tour in 1847.


1 Answers

Here's just a rough outline of what you should do:

  1. Make a "Square" class with fields for up, down, left, and right (plus accessor and modifier methods)
  2. Make a "Chessboard" class to store all the squares and set them up.
  3. Make a "Knight" class to move around the chessboard (and check if moves are valid).
  4. Finally, create a driver class that searches and stores how to move the Knights around.

Sample Square class:

public class Square
{
    public final Square up;
    public final Square down;
    public final Square left;
    public final Square right;
    public Square(Square up, Square down, Square left, Square right)
    {
        this.up=up;
        this.down=down;
        this.left=left;
        this.right=right;
    }
    public Square getUp(){return up;}
    public Square getDown(){return down;}
    public Square getLeft(){return left;}
    public Square getRight(){return right;}
}

Sample Knight class:

public class Knight
{
    private Square mySquare;

    public Knight(Square start)
    {
        mySquare = start;
    }

    /*  7 0
     * 6    1
     * 
     * 5    2
     *  4 3   
     */
    public boolean move(int dir)
    {
        switch(dir)
        {
        case 0: try{
           mySquare=mySquare.getUp().getUp().getRight(); return true;
        } catch (NullPointerException e) {return false}
        case 1: try{
           mySquare=mySquare.getUp().getRight().getRight(); return true;
        } catch (NullPointerException e) {return false}
        case 2: try{
           mySquare=mySquare.getDown().getRight().getRight(); return true;
        } catch (NullPointerException e) {return false}
        case 3: try{
           mySquare=mySquare.getDown().getDown().getRight(); return true;
        } catch (NullPointerException e) {return false}
        case 7: try{
           mySquare=mySquare.getUp().getUp().getLeft(); return true;
        } catch (NullPointerException e) {return false}
        case 6: try{
           mySquare=mySquare.getUp().getLeft().getLeft(); return true;
        } catch (NullPointerException e) {return false}
        case 5: try{
           mySquare=mySquare.getDown().getLeft().getLeft(); return true;
        } catch (NullPointerException e) {return false}
        case 4: try{
           mySquare=mySquare.getDown().getDown().getLeft(); return true;
        } catch (NullPointerException e) {return false}
        default: return false;
        }
    }
}
like image 179
Blue Avatar answered Sep 26 '22 22:09

Blue