Exemplo n.º 1
0
bool LocalSolver::applyMinConflicts() {
	for (int i = 0; i < this->MAX_STEPS; i++) {
		if (isMatrixValid())
			return true;

		//Apply min-conflicts.
		default_random_engine generator(random_device{}());
		shuffle(this->variables.begin(), this->variables.end(), generator);
		int index = 0;
		Variable var = this->variables[index++];
		while (SudokuMatrix::checkValidCell(this->matrix,var.getRow(), var.getCol())) {
			var = this->variables[index++];
		}
		int bestValue = -1, lowestConflicts = INT_MAX;
		for (int i = 0; i < var.getPossibleValues().size(); i++) {
			int conflicts = countConflicts(var.getRow(), var.getCol(), var.getPossibleValues()[i]);
			if (conflicts < lowestConflicts) {
				bestValue = var.getPossibleValues()[i];
				lowestConflicts = conflicts;
			}
		}

		//Assign
		matrix->setMatrixCell(this->variables[index - 1].getRow(),this->variables[index-1].getCol(),bestValue);
		this->variables[index - 1].setValue(bestValue);
	}

	return false;
}
Exemplo n.º 2
0
bool variableSort(Variable v, Variable v2) {
	if (v.getRow() == v2.getRow())
		return v.getCol() < v2.getCol();

	return v.getRow() < v2.getRow();
}