Ejemplo n.º 1
0
SudokuBoardSolver::SudokuBoardSolver(SudokuFile &sF, long timeout, bool ForwardCheck, bool DegreeHeuristic, bool LeastConstraintValue, bool MinimumRemainingValue)
:ForwardCheck(ForwardCheck), DegreeHeuristic(DegreeHeuristic), LeastConstraintValue(LeastConstraintValue), MinimumRemainingValue(MinimumRemainingValue)
{
	SudokuFile file(sF.getN(), sF.getP(), sF.getQ());
	if (ForwardCheck || DegreeHeuristic || LeastConstraintValue || MinimumRemainingValue){
		makeConstraintMap(sF);
		solutionType = forwardChecking(sF, timeout);
	}
	else{
		solutionType = backtrackingSearch(sF, timeout);
	}
	if (solutionType == 2 || solutionType == 3){
		sf.setN(sF.getN());
		sf.setBoard(file.getBoard());
	
	}
}
Ejemplo n.º 2
0
int SudokuBoardSolver::backtrackingSearch(SudokuFile &sF, long timeout)
{
	if (countOnce == 0)
	{
		startTime = std::chrono::system_clock::now().time_since_epoch();

	}

	countOnce++;

	bool timedOut = false;

	if (isComplete(sF))
	{
		sf = sF;
		return 1;
	}
	else
	{
		std::vector<std::vector<int>> board = sF.getBoard();

		for (int i = 0; i < sF.getN(); i++)
		{

			for (int j = 0; j < sF.getN(); j++)
			{

				for (int k = 1; k < sF.getN() + 1; k++)
				{

					if (board[i][j] == 0)
					{
		
						if (checkConstraints(i, j, k, sF, board))
						{
		
							board[i][j] = k;
							++countNode;
							sF.setBoard(board);
				

							if (backtrackingSearch(sF, timeout) == 1)
							{
								++countNode;
								return 1;
							}
							else
							{
								auto currentTime = std::chrono::system_clock::now().time_since_epoch();
								if (std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count() >= timeout)
								{
									++countNode;
									return 3;
								}

								else
								{
									board[i][j] = 0;
									sF.setBoard(board);
		
									if (k == sF.getN())
									{
										++backtrack;
										return 2;
									}
								}
							}
						}
						else if (k == sF.getN())
						{
							++backtrack;
							return 2;
						}

						auto currentTime = std::chrono::system_clock::now().time_since_epoch();
						if (std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count() >= timeout)
						{
							++countNode;
							return 3;
						}
					}
				}
			}
		}
	}

	return 2;

}
Ejemplo n.º 3
0
int main(int argc, char** argv)
{
	int method = 0;
	double start, finish;

	srand((unsigned int)time(NULL));
	
	fopen_s(&result, "mapColoring.txt", "w");
	if(result == NULL)
		printf("ERROR: File Open Failed");

	// Create Map Information
	printf("Start Map Generating..\n");
	createPoint();

	printf("Start Line Generating..\n");
	createEdge();
	if(boolAllEdge)
		createAllEdge();	// Plus Edge incremently..

	if(boolLog) {
		int a = 0;
		for(int i=0; i<POINT_COUNT; i++)
			a += mapPoints[i].edgeCount;
		printf("\n Created Edges Count:%d  \n", a/2);
	}
	
	// Save the Result
	if(boolSave) saveFile();
	
		initColor();
	// Select Color using each strategy
	for(method = 0; method < 3; method++) { // method 0~2
		kColor = 3;
		printf("\nStart map Coloring using %s\n", method == 0? "Backtracking" 
			: method == 1? "Backtracking using Foward check" : "Backtracking using Arc Consistency");
		if(boolSave) fprintf(result, "\nStart map Coloring using %s\n", method == 0? "Backtracking"
			: method == 1? "Backtracking using Foward check" : "Backtracking using Arc Consistency");

		// time check
		start = clock();

		initColor();
		if(!backtrackingSearch(method)) {
			printf("ERROR: It can't make %d-Coloring Map!!\n", kColor);
			if(boolSave) fprintf(result, "ERROR: It can't make %d-Coloring Map!!\n", kColor);
		
			kColor = 4;
			if(boolLog) printf("\nStart %d-Colorings Search\n", kColor);
			if(boolSave) fprintf(result, "\nStart %d-Colorings Search\n", kColor);

			initColor();
			backtrackingSearch(method);	// mapColoring using k = 4
		}
		
		printf("Map Coloring using %s is finished!!\n", method == 0? "Backtracking" 
			: method == 1? "Backtracking using Foward check" : "Backtracking using Arc Consistency");
		if(boolSave) fprintf(result, "Map Coloring using %s is finished!!\n", method == 0? "Backtracking" 
			: method == 1? "Backtracking using Foward check" : "Backtracking using Arc Consistency");

		// end time
		finish = clock();

		printf("time : %.3fs\n", ((finish-start)/CLOCKS_PER_SEC));
		if(boolSave) fprintf(result, "%s time : %.3fs\n"
			, method == 0? "Backtracking" : method == 1? "Backtracking using Foward check" : "Backtracking using Arc Consistency"
			, ((finish-start)/CLOCKS_PER_SEC));
		//printf("time : %f, %f, %.3f\n", start, finish, difftime(finish, start));
	}

	
	fclose(result);
	
	if(boolShowWindow) {
		glutInit(&argc, argv);
		mapDisplay();
	}
	return 0;
}