Java 骑士';s巡视:无法解决

Java 骑士';s巡视:无法解决,java,Java,我写这段代码是为了打印一个可能的骑士之旅,这样每个地方都只访问一次 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,i

我写这段代码是为了打印一个可能的骑士之旅,这样每个地方都只访问一次

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();


   }
}


我怀疑发生的是,您的第二个程序实际上工作正常。这两个程序都使用暴力搜索,效率极低,但第一个程序碰巧很快就遇到了一次巡演,而第二个程序没有。请尝试此代码-它与您的代码完全相同,但它会打印出巡演的进度。在我看来,它实际上是有效的,只是需要很长的时间来涉过所有的选择

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();
  }
}
static int move[][]=new int[8][8];
//静态int X[]={1,2,2,1,-1,-2,-2,-1};
//静态int Y[]={2,1,-1,-2,-2,-1,1,2};
静态int X[]={-1,1,2,2,1,1,2,2};
静态int Y[]={2,2,1,-1,-2,-2,-1,1};
静态布尔printMove(整数x、整数y、整数步长、字符串堆栈){
如果(步骤==65){
返回true;
}否则{
int-x1,y1;
对于(int l=0;l<8;l++){
System.out.println(stack+“step=“+step+”l=“+l”);
x1=x+x[l];
y1=y+y[l];
如果(x1<8&&y1<8&&x1>=0&&y1>=0&&move[x1][y1]=0){
移动[x1][y1]=步进;
if(打印移动(x1,y1,步骤+1,堆栈+“,”+l)){
返回true;
}否则{
移动[x1][y1]=0;
}
}
}
返回false;
}
}
静态void printSteps(){
对于(int i=0;i<8;i++){
对于(int j=0;j<8;j++){
系统输出打印(移动[i][j]+“”);
}
System.out.println();
}
}
公共静态void main(字符串[]args)引发InterruptedException{
试一试{
测试=新测试();
test.test();
移动[0][0]=1;
printMove(0,0,2,“”);
打印步骤();
}捕获(例外e){
e、 printStackTrace();
}
}

嗯。。。那么你的问题是什么?“不工作”是什么意思?什么不工作?问题是什么?编译或运行时没有错误,但代码永远不会完成-只是挂起(我想最终会溢出)。至少,当我试着运行它时会发生这种情况@Saurav,这是您应该在问题中包含的一些附加信息,以供将来参考:)如果(x1=0&&move[x1][y1]==0{…}添加一个
else{System.out.println(“不能移动!x1=“+x1+”y1=“+y1”step=“+step”);
并查看它打印的内容。
         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 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();
  }
}