Example #1
0
void sudokuTest() {
  vector< vector<int> > _grid = 
  {{3, 0, 6, 5, 0, 8, 4, 0, 0},
    {5, 2, 0, 0, 0, 0, 0, 0, 0},
    {0, 8, 7, 0, 0, 0, 0, 3, 1},
    {0, 0, 3, 0, 1, 0, 0, 8, 0},
    {9, 0, 0, 8, 6, 3, 0, 0, 5},
    {0, 5, 0, 0, 9, 0, 6, 0, 0},
    {1, 3, 0, 0, 0, 0, 2, 5, 0},
    {0, 0, 0, 0, 0, 0, 0, 7, 4},
    {0, 0, 5, 2, 0, 6, 3, 0, 0}};

  Sudoku* puzzle = new Sudoku( _grid );
  if(!puzzle->IsLegalBoard()) {
    drunkout("Illegal board.", cERROR);
  }
  puzzle->print();

  if(puzzle->solve())
    puzzle->print();
  else
    drunkout("No solution.", cINFO);

  delete puzzle;

}
int main() {
    string board;
    while (getline(cin, board)) {
        Sudoku game = board;
        game.print();
        game.solve();
        game.print();
        cout << game.exportString() << endl;
    }
    return 0;
}
Example #3
0
void Solver::guess(int index) { //Where slot=0-80
	Sudoku s = Sudoku();
	s.setField(_field);
	s.print();
	usleep(300);
	bool succes = false;
	for (int option : missing[index]) {
		if (_field.get(index) >= option) { // We have tried this slot before
			continue;
		}
		if (_field.fit(index, option)) {
			_field.set(index, option);
			succes = true;
			break;
		}
	}
	if (succes) {
		if (_field.countEmpty(true) > 0) {
			// Find the the next empty slot in the vector
			int curIndex = std::distance(emptySlots.begin(), std::find(emptySlots.begin(), emptySlots.end(), index));
			guess(emptySlots.at(curIndex + 1));
		} else {
			return;
		}
	} else {
		_field.set(index, 0);
		// Find the the previous tried slot in the vector
		int curIndex = std::distance(emptySlots.begin(), std::find(emptySlots.begin(), emptySlots.end(), index));
		guess(emptySlots.at(curIndex - 1));
	}
}
Example #4
0
void printProgress(int &count, Field &_field) {
	++count;
	if (count % 100000 == 0) {
		count = 0;
		Sudoku s = Sudoku();
		s.setField(_field);
		s.print();
		//usleep(300);
	}
}
Example #5
0
int main(int argc, char** argv) {
	if (argc != 3) {
		puts("Format: main.out input_filename output_filename");
		exit(1);
	}
	char* infilename = argv[1];
	char* outfilename = argv[2];
	Sudoku sudoku;
	sudoku.load(infilename, SUDOKUSIZE, SUDOKUUNITSIZE);
	sudoku.solve();
	sudoku.print(outfilename);
	return 0;
}
Example #6
0
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);
}