재귀 backtracking 알고리즘을 사용하여 주어진 sudoku 퍼즐을 풀려고합니다. 내 스도쿠 해결사에 두 가지 문제가 있습니다. 첫째로, 퍼즐을 해결하지만, 백업을 재발행하고 프로세스에서 언 솔트합니다 (약 4718 개의 재귀를 해결하고 다른 이유로 10000 개 정도를 백업합니다). 두 번째 문제는이 문제를 해결하려는 시도에서 비롯됩니다. 나는 그것을 발견하면 솔루션 나는이처럼 보이는 isSolved 방법을 사용하여 발견 검증 솔루션을 보유하는 글로벌 매트릭스를 사용Sudoku recursion with backtracking
public static boolean isSolved(int[][]puzzle){
for(int i = 0; i<9; i++){ //y rotation
for(int j = 0; j<8; j++){
if(puzzle[i][j]==0){
return false;
}
}
}
return true;
}
경우, 내 퍼즐, 0에있는 블록 그것은 비어있는 것과 같습니다. 그러나 이것은 리셋이되는 것으로 보이지만 어디에서 리셋되고 있는지 찾을 수는 없습니다. 포인터 또는 제안이나 포인터 어디에서 봐야합니까? 여기
내 해결 방법, 내가 다시 시간 내 주셔서 감사 중요한 라인public static int[][] solve(int[][]puzzle, int x, int y){
System.out.println("RecrDepth: " + recDepth);
recDepth++;
//using backtracking for brute force power of the gods(norse cause they obviously most b.a.
ArrayList<Integer> list = new ArrayList<Integer>();
//next for both x and y
int nextx = getNextx(x);
int nexty = getNexty(x, y);
while(puzzle[y][x] != 0){ //progress until blank space
x = nextx;
y = nexty;
if(isSolved(puzzle)){
System.out.println("resetting solution improperly");
solution = puzzle;
return puzzle;
}
nextx = getNextx(x);
nexty = getNexty(x, y);
}
for(int i = 1; i<10; i++){
if(isTrue(puzzle, y, x, i)) //isTrue checks column, row and box so we dont go down unnecessary paths
list.add(i);
}
for(int i=0; i<list.size(); i++){ //iterating through options in open squre recursing for each one
puzzle[y][x]= list.get(i);
if(isSolved(puzzle)){
System.out.println("Resetting Solution"); //appears to reset solution here but only once that I see in print out
solution = puzzle;
return puzzle;
}
System.out.print("=");
puzzle = solve(puzzle, nextx, nexty);
puzzle[y][x] = 0;//clear spot upon backtracking THIS WAS WHAT I WAS MISSIN
}
return puzzle;
}
주석을 첨부하고, 전체 코드를 읽는데 파일 wechtera에서 GitHub의에/ssolverOO이는 ssolver.java 파일이고 읽기는 ssolverin.txt입니다.
는 "이 리셋되는 것"을 의미 어떻게 : 참고로
, 여기에 내 isValid() 메소드가 isTrue()에 해당하는 곳에 내가 쓴 방법을 해결입니까? – bas
그것은 그것을 해결하고 원래의 포인트 만 남아있는 원래의 빈 퍼즐로 되돌립니다. –