2015-11-22 26 views
1

我正在研究一個應該輸入一個解決的Sudoku拼圖的程序,如果它是一個有效的解決方案或不是(真或假),則返回。 我的代碼是用一些幫助方法編寫和運行的。數獨調試問題

isSolution方法運行4個不同的事情來檢查解決方案是否有效。 我寫了一個有效的解決方案作爲輸入,應該返回true。 當我分別檢查這四個元素時,他們返回true,當我檢查它們在一起時,它們返回false(這是錯誤的)

我花了數小時分別測試它們,一起並以不同的組合進行測試。 我嘗試過不同的輸入。 我不明白爲什麼它應該返回true時返回false。

任何幫助將被驚人地讚賞!由於

public static void main(String[] args){ 
    int [][] solvedPuzzle = { 
    {8,3,5,4,1,6,9,2,7}, 
    {2,9,6,8,5,7,4,3,1}, 
    {4,1,7,2,9,3,6,5,8}, 

    {5,6,9,1,3,4,7,8,2}, 
    {1,2,3,6,7,8,5,4,9}, 
    {7,4,8,5,2,9,1,6,3}, 

    {6,5,2,7,8,1,3,9,4}, 
    {9,8,1,3,4,5,2,7,6}, 
    {3,7,4,9,6,2,8,1,5} 
    }; 

System.out.println(isSolution(solvedPuzzle)); 
} 


////// Checks if the input is a valid sudoku solution 
/* The solvedPuzzle input is a valid solution, so this method should return true. 
* Each of the elements in this method return true when tested individually, but for some reason, 
* when I run them all together, the method returns false 
*/ 
public static boolean isSolution(int [][] solvedPuzzle){ 
    //Checks if the rows and columns have 9 ints 
    if (solvedPuzzle.length != 9 || solvedPuzzle[0].length !=9){ 
    return false; 
    } 

//Checks if every column is made up of unique entries 
    for (int j = 0; j < 9; j++){ 
    if (uniqueEntries(getColumn(solvedPuzzle, j)) !=true){ 
     System.out.println("HERE!"); //these are just here to try to figure out WHERE I've gone wrong 
     return false; 
    } 
    } 

    //Checks if every row is made up of unique entries 
    for (int i = 0; i < 9; i++){ 
    if (uniqueEntries(solvedPuzzle[i]) !=true){ 
       System.out.println("HERE!!!"); 
     return false; 
    } 
    } 

    //Checks if every sub 3x3 grid is made up of unique entries 
    for (int x = 0; x < 9; x = x+3){ 
    for (int y = 0; y < 9; y = y+3){ 
     if (uniqueEntries(flatten(subGrid(solvedPuzzle, x,y,3))) != true){ 
        System.out.println("HERE22"); 
     return false; 
     } 
    } 
    } 

return true; 
} 




///Below are the helper methods 

////// Creates a smaller grid of size m starting at indexI,indexJ (x,y). 
public static int [][] subGrid(int [][] original, int indexI, int indexJ, int m){ 
    int [][] subGrid = new int [m][m]; 
    for (int i = indexI; i < indexI+m ; i++){ 
     for (int j = indexJ; j < indexJ+m ; j++){ 
      subGrid [i - indexI][j - indexJ] = original[i][j]; 
     } 
    } 
    return subGrid; 
} 

////// Sorts the intergers in a 1D array in asceding order 
public static int [] sort(int [] originalArray){ 
    int temp; 
    for(int i = 0; i < originalArray.length - 1; i++){ 
     for(int j = 0; j < originalArray.length - 1; j++){ 
      if(originalArray[j] > originalArray[j+1]){ 
       temp = originalArray[j]; 
       originalArray[j] = originalArray[j+1]; 
       originalArray[j+1] = temp; 
      } 
     } 
    } 
    return(originalArray); 
} 

////// Checks if the intergers in a 1D array are all unique by first using the sort method 
public static boolean uniqueEntries(int [] original){ 
    int [] sorted = sort(original); 
    for (int i = 0; i < original.length-1; i++){ 
    if (sorted[i+1] == sorted[i]) { 
     return false; 
     } 
    } 
    return true; 
} 

////// Takes a 2D array where each subarray is of the same size and creates a 1D array made up of the i-th element of each sub array 
public static int [] getColumn(int [][] original, int indexJ){ 
    int [] column = new int[original[0].length]; 
    for (int i = 0; i < original[0].length; i++){ 
     column[i] = original[i][indexJ]; 
    } 
    return column; 
} 

////// takes a 2D array and flattens it into a 1D array 
public static int [] flatten(int [][] original){ 
    int [] flattenedArray = new int[original.length*original[0].length]; 
    int counter = 0; 
    for (int i = 0; i < original.length; i++){ 
    for(int j = 0; j < original.length; j++) { 
     flattenedArray[counter] = original[i][j]; 
     counter++; 
     } 
    } 
    return flattenedArray; 
} 
+0

'I'original.length,1'和不是'我 789

+0

如果在'isSolution'方法內交換'//檢查每行是否由唯一條目組成的塊'與''檢查'isSolution'方法內的'檢查每個子3x3網格是否由唯一條目構成'的塊,您將看到它的工作原理。這應該給你一個提示問題的地方! – Rakim

+0

@Rakim它的工作! ...仍然不明白爲什麼,只是切換這些步驟的順序似乎已經解決了問題,而無需更改代碼中的其他任何內容。 非常感謝! –

回答

1

,如果您檢查您的難題之前運行//Checks if every row is made up of unique entries後,你會看到,你實際上是在改變你的拼圖的原始格式。所以下一個測試不會跑過原來的拼圖,而是一個排序的拼圖!如果您之前添加一個簡單的循環和第二次測試後,你就會明白我說的上述約

for (int i = 0; i < 9; i++){ //ADD THAT LOOP BEFORE AND AFTER THE TEST 
    for (int j = 0; j<9; j++) { 
     System.out.print(solvedPuzzle[i][j]); 
    } 
    System.out.println(); 
    } 
    System.out.println('\n'); 

    //Checks if every row is made up of unique entries 
    for (int i = 0; i < 9; i++){ 
    if (uniqueEntries(solvedPuzzle[i]) !=true){ 
       System.out.println("HERE!!!"); 
     return false; 
    } 
    } 

    for (int i = 0; i < 9; i++){ 
    for (int j = 0; j<9; j++) { 
     System.out.print(solvedPuzzle[i][j]); 
    } 
    System.out.println(); 
    } 

代碼將幫助您直觀地想象拼圖測試之前和之後看起來。測試不應該改變任何被測試的格式/內容/屬性。上述結果將爲:

835416927 
296857431 
417293658 
569134782 
123678549 
748529163 
652781394 
981345276 
374962815 


123456789 
123456789 
123456789 
123456789 
123456789 
123456789 
123456789 
123456789 
123456789 

正如你所看到的,原來的拼圖不再是「原創」了。

所以正如我在評論中告訴你的,翻轉測試並不能真正解決問題。相反,錯誤仍然會產生;但不會影響CURRENT代碼後運行任何測試。

希望幫助

編輯:我不知道我是否會在網上以後;所以在這種情況下,即使這個提示沒有幫助你找到bug,所以我也會給你解決方案:p

問題是你使用的sort()方法不只返回數組排序,但也實際上排序輸入數組!所以爲了避免這種情況,你只需要在陣列而非陣列本身,只要你所呼叫的排序方法的副本通:你爲什麼要使用

////// Checks if the intergers in a 1D array are all unique by first using the sort method 
    public static boolean uniqueEntries(int [] original){ 
     int [] sorted = sort(original.clone()); //pass in a copy of the array 
     for (int i = 0; i < original.length-1; i++){ 
      if (sorted[i+1] == sorted[i]) { 
       return false; 
      } 
     } 
     return true; 
    }