• Source
    1. /* package whatever; // don't place package name! */
    2.  
    3. import java.util.*;
    4. import java.lang.*;
    5. import java.io.*;
    6.  
    7. /**
    8.  * knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once.
    9.  * If the knight ends on a square that is one knight's move from the beginning square
    10.  * (so that it could tour the board again immediately, following the same path), the tour is closed, otherwise it is open.
    11.  *
    12.  * Knights tour problem : http://en.wikipedia.org/wiki/Knight%27s_tour
    13.  */
    14. class Ideone {
    15. public static void main(String[] args){
    16. int n = 8;
    17.  
    18. //knight can move 3cells in L shape at a time
    19. //Prepare combinations of possible x&y direction moves of knight
    20.  
    21. //below array represents possible moves in x&y directions with values (x[i],y[i]) and sum will be 3
    22. int[] x = {1, 2, -1, -2, 1, -2, -1, 2};
    23. int[] y = {2, 1, -2, -1, -2, 1, 2, -1};
    24.  
    25. //Initialize a matrix to hold the path solution
    26. int[][] solution = new int[n][n];
    27.  
    28. //Initialise solution matrix to default values. default value is -1
    29. for(int i=0; i <n; i++){
    30. for(int j=0; j< n; j++){
    31. solution[i][j] = -1;
    32. }
    33. }
    34.  
    35. //Start solving problem with move from (0,0) position
    36. solution[0][0] = 0;
    37.  
    38. boolean solutionFound = trackKnightMoves(0, 0, x, y, solution, 1);
    39. if(solutionFound){
    40. printSolutions(solution);
    41. }
    42. }
    43.  
    44. private static void printSolutions(int[][] sol){
    45. for(int i=0; i< sol.length; i++){
    46. for(int j=0; j< sol.length; j++){
    47. System.out.println(" " + sol[i][j]+ " ");
    48. }
    49. }
    50. }
    51.  
    52. /**
    53. * curX - to track current x position of knight
    54. * curY - to track current y position of knight
    55. * x - possible x direction moves
    56. * y - possible y direction moves
    57. * solution - to track path of knight
    58. * noOfMoves - count of moves in a solution(to track end of traversal)
    59. */
    60. private static boolean trackKnightMoves(int curX, int curY, int[] x, int[] y, int[][] solution, int noOfMoves){
    61.  
    62. if(noOfMoves == solution.length * solution.length){
    63. System.out.println("No of moves => "+ noOfMoves);
    64. return true;
    65. }
    66.  
    67. for(int k=0; k<solution.length; k++){
    68. int nextXMove = curX + x[k];
    69. int nextYMove = curY + y[k];
    70.  
    71. //Check if next move is safe or not
    72. if(isSafeMove(nextXMove, nextYMove, solution)){
    73. solution[nextXMove][nextYMove] = noOfMoves;
    74. if(trackKnightMoves(nextXMove, nextYMove, x, y, solution, noOfMoves+1)){
    75. return true;
    76. }else{//back track
    77. solution[nextXMove][nextYMove] = -1;
    78. }
    79. }
    80. }
    81. return false;
    82. }
    83.  
    84. /*
    85. * Before taking next step, check whether it is safe to move in that direction or not
    86. */
    87. private static boolean isSafeMove(int x, int y, int[][] solution){
    88. if(x >=0 && x<solution.length && y >= 0 && y < solution.length && solution[x][y] == -1){
    89. return true;
    90. }else {
    91. return false;
    92. }
    93. }
    94. }
    95.