1

I wrote this code to print one of the possible knight's tour such that every place is visited exactly once.

public class Main{
         static int move[][]=new int[8][8];
         static int X[]={1 , 2 , 2 , 1 ,-1 ,-2 ,-2,-1};
         static int Y[]={2 , 1 ,-1 ,-2 ,-2 ,-1 , 1, 2};
         static boolean printMove(int x,int y,int step){
   if(step==65){

       return true;
   }
   else{
       int x1,y1;

       for(int l=0;l<8;l++){
           x1=x+X[l];
           y1=y+Y[l];
           if(x1<8&&y1<8&&x1>=0&&y1>=0&&move[x1][y1]==0){

               move[x1][y1]=step;
               if(printMove(x1,y1,step+1)){
                   return true;
               }
               else
               move[x1][y1]=0;


           }
       }
       return false;

   }
} 

 static void printSteps(){
       for(int i=0;i<8;i++){
          for(int j=0;j<8;j++){
               System.out.print(move[i][j]+" ");
             }
           System.out.println();
             }
    }
public static void main(String args[]){

     move[0][0]=1;

     printMove(0,0,2);   
     printSteps();


   }
}

this code is working but the following code is not working, I made little change in X[] and Y[] which should not effect the algorithm.

    public class Main{
         static int move[][]=new int[8][8];
         static int X[]={-1, 1 , 2 , 2 , 1 ,-1 ,-2 ,-2};
         static int Y[]={ 2, 2 , 1 ,-1 ,-2 ,-2 ,-1 , 1};
         static boolean printMove(int x,int y,int step){
   if(step==65){

       return true;
   }
   else{
       int x1,y1;

       for(int l=0;l<8;l++){
           x1=x+X[l];
           y1=y+Y[l];
           if(x1<8&&y1<8&&x1>=0&&y1>=0&&move[x1][y1]==0){

               move[x1][y1]=step;
               if(printMove(x1,y1,step+1)){
                   return true;
               }
               else
               move[x1][y1]=0;


           }
       }
       return false;

   }
} 

 static void printSteps(){
       for(int i=0;i<8;i++){
          for(int j=0;j<8;j++){
               System.out.print(move[i][j]+" ");
             }
           System.out.println();
             }
    }
public static void main(String args[]){

     move[0][0]=1;

     printMove(0,0,2);   
     printSteps();


   }
}

I just changed

         static int X[]={1 , 2 , 2 , 1 ,-1 ,-2 ,-2,-1};
         static int Y[]={2 , 1 ,-1 ,-2 ,-2 ,-1 , 1, 2};

to

         static int X[]={-1, 1 , 2 , 2 , 1 ,-1 ,-2 ,-2};
         static int Y[]={ 2, 2 , 1 ,-1 ,-2 ,-2 ,-1 , 1};
false
  • 10,264
  • 13
  • 101
  • 209
Saurav Kumar Singh
  • 1,352
  • 7
  • 18
  • 1
    Uh... so what's your question? What do you mean by "not working"? – millimoose Nov 21 '12 at 15:54
  • What's not working? What's the problem? – Force444 Nov 21 '12 at 15:55
  • 1
    There are no errors when compiling or running, but the code never completes- just hangs (and eventually will overflow, I assume). At least, that's what happens when I try running it... @Saurav, this is some additional information you should include in your question, for future reference :) – username tbd Nov 21 '12 at 16:00
  • Where you do your `if(x1<8&&y1<8&&x1>=0&&y1>=0&&move[x1][y1]==0){...}}` add an `else{System.out.println("Can't move! x1="+x1+" y1="+y1" step="+step);` and see what it prints. – OldCurmudgeon Nov 21 '12 at 16:32

2 Answers2

0

What I suspect happens is that your second program actually works correctly. Both programs use brute force search and are extremely inefficient, but the first one happens to stumble upon a tour fairly quickly, whereas the second one doesn't.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Try this code - it's exactly the same as you have but it prints it's progress. It looks to me like it is actually working, it is just taking a long time to wade through all of the alternatives.

static int move[][] = new int[8][8];
//static int X[]={1 , 2 , 2 , 1 ,-1 ,-2 ,-2,-1};
//static int Y[]={2 , 1 ,-1 ,-2 ,-2 ,-1 , 1, 2};
static int X[]={-1, 1 , 2 , 2 , 1 ,-1 ,-2 ,-2};
static int Y[]={ 2, 2 , 1 ,-1 ,-2 ,-2 ,-1 , 1};
static boolean printMove(int x, int y, int step, String stack) {
  if (step == 65) {

    return true;
  } else {
    int x1, y1;

    for (int l = 0; l < 8; l++) {
      System.out.println(stack+" step = "+step+" l = "+l);
      x1 = x + X[l];
      y1 = y + Y[l];
      if (x1 < 8 && y1 < 8 && x1 >= 0 && y1 >= 0 && move[x1][y1] == 0) {

        move[x1][y1] = step;
        if (printMove(x1, y1, step + 1, stack + "," + l)) {
          return true;
        } else {
          move[x1][y1] = 0;
        }
      }
    }
    return false;

  }
}

static void printSteps() {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      System.out.print(move[i][j] + " ");
    }
    System.out.println();
  }
}

public static void main(String[] args) throws InterruptedException {
  try {
    Test test = new Test();
    test.test();
    move[0][0] = 1;

    printMove(0, 0, 2, "");
    printSteps();
  } catch (Exception e) {
    e.printStackTrace();
  }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213