예제 #1
0
  private static void solve(Grid grid, List<Grid> solutions) {
    // Return if there is already more than two solution
    if (solutions.size() >= 2) {
      return;
    }

    // Find first empty cell
    int loc = grid.findEmptyCell();

    // If no empty cells are found,a solution is found
    if (loc < 0) {
      solutions.add(grid.clone());
      return;
    }

    // Try each of the 9 digits in this empty cell
    for (int n = 1; n < 10; n++) {
      if (grid.set(loc, n)) {
        // With this cell set,work on the next cell
        solve(grid, solutions);

        // Clear the cell so that it can be filled with another digit
        grid.clear(loc);
      }
    }
  }