2017-09-13 11 views
0

스퀘어 크기의 보드 (4x4, 9x9, 16x6 등)에서 작동하는 스도쿠 해결사를 구현하려고합니다. 현재 알고리즘에 대한 것이지만 일단 값을 실행하지 않으면 끝납니다. 변경된 theGrid는 해결이 실행되기 전과 동일합니다.
나는 그것이 어디로 잘못 가고 어떤 도움을 주신지 정말로 모른다.스도쿠 (Sudoku) 해결사가 값을 변경하지 않음

private int[][] theGrid; 
private int emptyValue = -1; 

public int[][] solve() { 
    recSolve(0, 0); 
    return theGrid; 
} 


void recSolve(int i, int j) { 
    int size = theGrid.length; 
    if (i == size) { 
     i = 0; 
     if (++j == size) 
      return; 
      //done 
    } 
    if (theGrid[i][j] != emptyValue) // skip filled cells 
     recSolve(i+1,j); 

    for (int val = 1; val <= size; ++val) { 
     if (!isPresent(theGrid, i, j, val)) { 
      theGrid[i][j] = val; 
      recSolve(i+1,j) 
     } 
    } 
    theGrid[i][j] = emptyValue; // reset on backtrack 
} 

boolean isPresent(int[][] grid, int row, int col, int num){ 
    for(int i = 0; i < theGrid.length; i++){ 
     if(theGrid[i][col] == num) return false; 
     if(theGrid[row][i] == num) return false; 
    } 
    int side = (int)Math.sqrt(theGrid.length); 

    int rowStart = row - row % side; 
    int colStart = col - col % side; 

    for(int m = 0; m < side; m++){ 
     for(int k = 0; k < side; k++){ 
      if(grid[rowStart + k][colStart + m] == num) return false; 
     } 
    } 
    return true; 
} 
+1

실제로 함수를 어딘가에 호출합니까? – Carcigenicate

+0

실제로 theGrid를 어딘가에 초기화합니까? –

+1

이 전체 파일의 코드를 붙여 넣을 수 있습니까? 코드 흐름을 쉽게 이해할 수 있습니다. – zenwraight

답변

0

디버거를 실행하는 것이 좋습니다. 곧 잘못된 것이 있습니다. 처음에는 isPresent이 값이 존재하지 않으면 true를 반환합니다. 이는 예상했던 것의 반대입니다.