2014-02-25 54 views
1

我收到一個錯誤的StackOverflow當過我運行該程序,而不是尋找騎士之旅。任何想法是什麼導致了這一點,以及我如何改變我的代碼,實際上找到騎士之旅,擺脫這個錯誤。項目是爲我的CS280類,並在週五到期,請幫助。謝謝!!騎士之旅的Java

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++;     
         } 
       } 
     } 
} 
+6

堆棧跟蹤將是很好,但如果你自己動手調試,這將讓你瘦比任何回答,我們可以提供多很多。 –

+0

在你的'findTour'方法中查看第一組'if'條件;有沒有一種途徑**不會**再次遞歸調用'findTour'? –

+1

請了解後增量和增量運算符。也許你會看到你的錯誤。 – Seelenvirtuose

回答

1

如果你的算法確實需要一個大深度的遞歸調用,您可以傳遞參數給JVM在啓動時增加限制堆棧大小:-Xss4m

當然,這隻會延緩如果你的算法沒有限制地遞歸,那麼問題就在這裏

0

你需要在你的開關case語句一些休息,否則會落空。

+0

我實現了break語句,並仍然得到這個在KnightsTour.move(KnightsTour.java:155)錯誤 – user2612136

+0

異常在線程 「主」 java.lang.StackOverflowError的 在KnightsTour.tryNextMove(KnightsTour.java:189) --- jGRASP wedge2:用於過程退出代碼是1. ---- jGRASP:操作完成。 – user2612136

+0

與if/elses案件的休息看起來不正確。你需要包含行號,以便我們可以看到189行。 – pecks