Skip to content

Instantly share code, notes, and snippets.

/Solve.java Secret

Created August 21, 2013 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f62cb9fea9c05ad6d023 to your computer and use it in GitHub Desktop.
Save anonymous/f62cb9fea9c05ad6d023 to your computer and use it in GitHub Desktop.
//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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment