void RecursiveSolver::solve(Sudoku& sudoku) { this->_field = sudoku.getField(); options = findMissing(); for(map<int,vector<int>>::iterator it = options.begin(); it != options.end(); ++it) { cout << it->first << " " << ": "; for (vector<int>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) { cout << *it2 << ", "; } cout << endl; } map<int,vector<int>>::iterator it = options.begin(); count = 0; int status = -1; do { status = guess(it->first); switch(status) { case RETURN_NEXT: it = std::next(it); break; case RETURN_PREV: it = std::prev(it); break; } } while(status != RETURN_EXIT); sudoku.setField(_field); }
void Solver::solve(Sudoku& sudoku) { this->_field = sudoku.getField(); if (_field.countEmpty() == 0) { cout << "RETURN SOLVE BECAUSE NO EMPTY SLOTS 1" << endl; return; } // Look for slots with only one option, fill it in and repeat bool hit; do { hit = false; // Find all possible options for the empty slots missing = findMissing(); // Find all slots with only one option for (int index = 0; index < _field.getSizeSq(); ++index) { if (missing[index].size() == 1) { _field.set(index, missing[index].at(0)); missing[index].clear(); cout << "MADE A HIT AT " << index << " WITH " << _field.get(index) << endl; hit = true; } } } while (hit == true); // We completed the puzzle, thus return if (_field.countEmpty(true) == 0) { cout << "RETURN SOLVE BECAUSE NO EMPTY SLOTS 2" << endl; sudoku.setField(_field); return; } // No more sure slots, so start guessing std::cout << "COMPLETED ALL SURE HITS, START GUESSING" << std::endl; // Print the result so far std::cout << "RESULT SO FAR: " << std::endl; Sudoku s = Sudoku(); s.setField(_field); s.print(); // Find all possible options for the remaining empty slots missing = findMissing(); // Print all options for (int index = 0; index < _field.getSizeSq(); ++index) { cout << "[" << index << "]: "; for (int index2 = 0; index2 < missing[index].size(); ++index2) { cout << missing[index].at(index2) << " "; } cout << "\n"; } emptySlots.clear(); // Fill in the empty slots to the array for(map<int,vector<int>>::iterator it = missing.begin(); it != missing.end(); ++it) { if ((it->second).size() > 0) emptySlots.push_back(it->first); } // Sort the emptyslot indices by size small->big std::sort(emptySlots.begin(), emptySlots.end(), lessThan); guess(emptySlots.at(0)); sudoku.setField(_field); }