2016-06-10 37 views
0

我收到了一些編程任務,以進入面試的下一步。他們都接受期待一,描述:https://academicwork.kattis.com/problems/pebblesolitaire2(自動代碼校正軟件)PebbleSolitaire解決方案未在Kattis中被接受(java)

雖然這是很長一段時間,因爲我用遞歸,我仍然認爲我想出了一個非常簡單和工作的解決方案。 (雖然可能不是最好的。)它處理問題描述中提供的所有「樣本輸入」,並根據我的情況提供正確的「輸出」。它仍然被拒絕,唯一的拒絕線索是「在測試文件2/7失敗:錯誤的答案」。沒有更多的繼續,我真的不知道我的代碼在哪裏發出錯誤的答案。

關於如何前進的建議?

import java.util.Scanner; 
public class PebbleSolitaire { 

private static int bestMove; 
private static char[] table; 

public static void main(String[] args) { 

    Scanner scan = new Scanner(System.in); 

    while (scan.hasNext()) { 
     int i = scan.nextInt(); 

     for (int j = 0; j < i; j++) { 
      bestMove = 23; 
      String a = scan.next(); 
      table = a.toCharArray(); 
      char[] testTable = a.toCharArray(); 
      checkMoves(testTable); 
      System.out.println(bestMove); 

     } 

    } 

} 

public static void checkMoves(char[] array) { 

    for (int i = 0; i < 22; i++) { 
     if (array[i] == 'o' && array[i + 1] == 'o') { 
      if (i + 2 < 23 && array[i + 2] == '-' && i - 1 >= 0 && array[i - 1] == '-') { 
       char[] tempArray; 
       tempArray = array; 
       tempArray[i - 1] = 'o'; 
       tempArray[i] = '-'; 
       tempArray[i + 1] = '-'; 
       checkMoves(tempArray); 

       table[i + 2] = 'o'; 
       table[i] = '-'; 
       table[i + 1] = '-'; 
       checkMoves(table); 

      } 
      if (i + 2 < 23 && array[i + 2] == '-') { 
       array[i + 2] = 'o'; 
       array[i] = '-'; 
       array[i + 1] = '-'; 
       checkMoves(array); 
      } 
      if (i - 1 >= 0 && array[i - 1] == '-') { 
       array[i - 1] = 'o'; 
       array[i] = '-'; 
       array[i + 1] = '-'; 
       checkMoves(array); 
      } 
     } 
    } 

    int counter = 0; 

    for (int i = 0; i < 23; i++) { 
     if (array[i] == 'o') { 
      counter++; 

     } 
    } 

    if (counter < bestMove) { 
     bestMove = counter; 
    } 

} 

}

回答

1

不知道你已經理解了它與否。

問題是您爲數組創建的變量引用了SAME對象。所以無論何時更改任何變量(無論是array還是tempArray),OBJECT的值都會發生變化。數組變量引用同一個對象。

相關問題