secret
anonymous / Solve.java
Created

  • Download Gist
Solve.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//TODO
public boolean search(Sudoku puzzle) {
puzzle.constraintPropagation();
if(puzzle.checkContradiction()) return false;
if(puzzle.checkSolved()) return true;
 
//for cell with fewest candidates, loop over candidates
Cell c = puzzle.getEasiest();
while(c.numCandidates() > 0) {
int candidate = c.getCandidates()[0];
Sudoku s = (Sudoku) puzzle.clone();
s.set(c.getRowNum(), c.getColNum(), candidate);
if(search(s)) {
puzzle = s;
System.out.println(s);
return true;
}
else c.remove(candidate);
}
return false;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.