N-Knights Tour

Hi friend, I am Java Learner, I would like to seek for help on my coding. The requirement is as below, The knights must visits the maximum number of board positions without attacking each other. The Knight can move in the shape of the letter, 'L', over two in one direction and then over one in a perpendicular direction. If the Knight rests at the square marked X. Please assist, thanks.

Package knightstour;
import java.util.*;

public class KnightsTour {
    private static int board[][] = new int[8][8];
    private static int stepCounter = 1;
   
    public KnightsTour() { 
        initBoard(board);
        tour(0,0); 
        printSol(board); 
    }

    public static void printSol(int[][] a) { 
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if(a[i][j]>9) { 
                    System.out.print(a[i][j] + " ");
                } else { 
                    System.out.print(a[i][j] + " ");
                } 
            }
        System.out.println(); } System.out.println();
       }

     public static void initBoard(int[][] a) {
         for (int i = 0; i < a.length; i++) {
             for (int j = 0; j < a[i].length; j++) { 
                 a[i][j] = -1; 
              } 
         } 
     }

     public void tour(int x, int y) {
         if (((x < 0) || (x >= 8) || (y < 0) || (y >= 8)) || (board[x][y] != -1)){ 
            return;
         } else { 
             stepCounter++;
             board[x][y] = stepCounter;
             tour(x+2, y+1);
             tour(x+1, y-2);
             tour(x+1, y+2);
             tour(x-1, y+2);
             tour(x-2, y-1);
             tour(x-2, y+1);
             tour(x-1, y-2);
             tour(x+2, y-1);
        } 
     }

     public boolean spaceAvailable(int X, int Y) { }

     Public static void main(String[] args) {
          new KnightsTour(); 
     }
}

 

Author: waynelaw
Login to Answer
Sort By:
Oldest
|
Newest
|
Vote
|
My Answer