Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 爪哇骑士团_Java_Recursion_Backtracking - Fatal编程技术网

Java 爪哇骑士团

Java 爪哇骑士团,java,recursion,backtracking,Java,Recursion,Backtracking,当我运行程序而不是查找骑士巡演时,我收到一个堆栈溢出错误。你知道是什么原因造成的吗?我如何改变我的代码来找到骑士之旅并消除这个错误。该项目是我的CS280课程,周五到期,请帮忙。谢谢 public class KnightsTourDriver { public static void main (String[]args) { KnightsTour tour1 = new KnightsTour; tour1.findTour(1);

当我运行程序而不是查找骑士巡演时,我收到一个堆栈溢出错误。你知道是什么原因造成的吗?我如何改变我的代码来找到骑士之旅并消除这个错误。该项目是我的CS280课程,周五到期,请帮忙。谢谢

public class KnightsTourDriver {
    public static void main (String[]args) {
        KnightsTour tour1 = new KnightsTour;
        tour1.findTour(1);
        tour1.displayTour();
    }
}  



import java.util.Arrays;
class KnightsTour
{

    static int the_board[][] = new int[8][8];
    int the_tour[][] = new int [8][8];
    int k,moves = 0;
    int x = 0, y = 0; 
    int z, j = 1;
    boolean tour_found, isSafe;

        //fills 2D array with 0's
        public KnightsTour()
        {
            for (int i = 0; i < 8; i++) 
                {
                 for (int r = 0; r < 8; r++) 
                    {
                            the_board[i][r] = 0;
                        }
                }
        }
        /*recursive method, checks how many moves were made if 16 were made tour finished, 
        else if not moves knight checks if the move is valid if not back tracks*/
        public void findTour(int q)
        {

            if(moves == 64)
                {
                    tour_found = true;
                }

            else 
                move(q); 

            if(isSafe == true)
                    {
                        findTour(q++);
                        moves++;
                    }
            else
                if(isSafe == false)
                    {
                        findTour(q*(-1));
                        moves--;
                    }
        }
        //used to keep prevent arrayindexoutofbounds error
        public boolean arrayInBounds(int x, int y)
        { 
        if(x < 8 && y < 8 && x >= 0 && y >= 0)
                {
                    return true;
                }
            else return false;
        }
        /*move method uses switch statement to decide which move the knight should make
        based on a case number, negative case numbers back track knight. if statement checks
        if the current array element is empty and the index is inbounds*/
        public void move(int a)
        {

            switch (a)
            {
               case 1:
                if(arrayInBounds(x+2, y+1) == true){
                   if(the_board[x+2][y+1] != 0){                          
                        the_board[x+2][y+1]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
                    }
                else isSafe = false;
               case 2:
                if (arrayInBounds(x+1, y+2) == true){
                    if(the_board[x+1][y+2] != 0){               
                            the_board[x+1][y+2]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
                }
                else isSafe = false;
               case 3:
                 if(arrayInBounds(x-1, y+2) == true){
                   if(the_board[x-1][y+2] != 0){           
                            the_board[x-1][y+2]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
                 }
                else isSafe = false;
               case 4:
                if (arrayInBounds(x-2, y+1) == true){
                    if(the_board[x-2][y+1] != 0){           
                            the_board[x-2][y+1]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
                }
                else isSafe = false;
               case 5:
                if(arrayInBounds(x-2, y-1) == true){
                    if(the_board[x-2][y-1] != 0){           
                            the_board[x-2][y-1]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
                }
                else isSafe = false;
               case 6:
                if(arrayInBounds(x-1, y-2) == true){
                        if(the_board[x-1][y-2] != 0){                    
                            the_board[x-1][y-2]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
            }
                else isSafe = false;
               case 7:
                 if(arrayInBounds(x+1, y-2) == true){
                    if(the_board[x+1][y-2] != 0){          
                            the_board[x+1][y-2]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
                 }
                 else isSafe = false;
               case 8:
                if(arrayInBounds(x+2, y-1) == true){
                 if(the_board[x+2][y-1] != 0){
                            the_board[x+2][y-1]=j;
                            j++;
                            break;
                        }
                        else isSafe = false;
                        break;
                }
                else isSafe = false;
               case -1:
                      j--;
                      tryNextMove(a);
                      break;
                    case -2:
                        j--;
                        tryNextMove(a);
                        break;
                    case -3:
                      j--;
                      tryNextMove(a);
                      break;
                    case -4:
                      j--;
                      tryNextMove(a);
                      break;
                  case -5:
                     j--;
                      tryNextMove(a);
                      break;
                    case -6:
                      j--;
                      tryNextMove(a);
                      break;
                    case -7:
                      j--;
                      tryNextMove(a);
                      break;
                   case -8:
                      j--;
                      tryNextMove(a);
                      break;
             }
        }
        public void tryNextMove(int m)
        {
            move(m++);
        }
        //for loop to display tour once found//         
        public void displayTour()
        {
            int v = 1;
            for (int i = 0; i < 8; i++) 
                {
                 for (int e = 0; e < 8; e++) 
                    {               
                                if(v % 8 == 0)
                                {
                                    System.out.print(the_board[i][e] + "\t");
                                    System.out.println("\n");
                                } 
                        else    
                            System.out.print(the_board[i][e] + "\t");
                            v++;                
                        }
                }
        }
}
KnightsTourDriver公共级{
公共静态void main(字符串[]args){

骑士之旅1=新骑士; 巡回赛1.findTour(1); tour1.displayTour(); } } 导入java.util.array; 骑士团 { 静态int_board[][]=新int[8][8]; int the_tour[]]=新int[8][8]; int k,moves=0; int x=0,y=0; intz,j=1; 布尔图库,isSafe; //用0填充二维数组 公共骑士团() { 对于(int i=0;i<8;i++) { 对于(int r=0;r<8;r++) { _board[i][r]=0; } } } /*递归方法,检查16次巡演结束时的移动次数, 否则,如果没有移动,骑士将检查移动是否有效,如果没有返回轨迹*/ 公共无效findTour(整数q) { 如果(移动==64) { tour_found=true; } 其他的 移动(q); 如果(isSafe==真) { findTour(q++); 移动++; } 其他的 if(isSafe==false) { findTour(q*(-1)); 移动--; } } //用于防止arrayindexoutofbounds错误 公共布尔数组边界(整数x,整数y) { 如果(x<8&&y<8&&x>=0&&y>=0) { 返回true; } 否则返回false; } /*move方法使用switch语句来决定骑士应该做出的移动 根据案例编号,负案例编号返回knight.if语句检查 如果当前数组元素为空且索引为inbounds*/ 公共无效移动(INTA) { 开关(a) { 案例1: if(arrayInBounds(x+2,y+1)=真){ 如果(U板[x+2][y+1]!=0){ _板[x+2][y+1]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例2: if(arrayInBounds(x+1,y+2)=真){ 如果(U板[x+1][y+2]!=0){ _板[x+1][y+2]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例3: if(arrayInBounds(x-1,y+2)=真){ 如果(U板[x-1][y+2]!=0){ _板[x-1][y+2]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例4: if(arrayInBounds(x-2,y+1)=真){ 如果(U板[x-2][y+1]!=0){ _板[x-2][y+1]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例5: if(arrayInBounds(x-2,y-1)=真){ 如果(U板[x-2][y-1]!=0){ _板[x-2][y-1]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例6: if(arrayInBounds(x-1,y-2)=真){ 如果(U板[x-1][y-2]!=0){ _板[x-1][y-2]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例7: if(arrayInBounds(x+1,y-2)=真){ 如果(U板[x+1][y-2]!=0){ _板[x+1][y-2]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例8: if(arrayInBounds(x+2,y-1)=真){ 如果(U板[x+2][y-1]!=0){ _板[x+2][y-1]=j; j++; 打破 } else-isSafe=false; 打破 } else-isSafe=false; 案例1: j--; tryNextMove(a); 打破 案例2: j--; tryNextMove(a); 打破 案例3: j--;