Exemplo n.º 1
0
void generateUniqueBoard(sudokuGrid *game) {
	int i, j;
	int positions[GRID_SIZE];
	int targetValue, position;
	
	//start by generating a random board
	resetBoard(game);
	generateRandomBoard(game);
	
	//populate and shuffle positions array - these are targets for cell removal
	j = 0;
	for (i = 0; i <= GRID_SIZE; i++) {
		positions[j] = i;
		j++;
	}
	shuffleArr(positions,GRID_SIZE);
	
	//clear cells until we no longer have a unique solution
	j = 0;
	while (j < (GRID_SIZE - GRID_SIZE/PRINCIPAL_NUM)) {
		if (solutionCount(game,2) > 1) {
			//restore the last cell that we removed to ensure that we still have a unique solution
			commitMove(game,position,targetValue);
		}
		
		targetValue = game->values[positions[j]];
		position = positions[j];
		
		commitMove(game,position,BLANK_VALUE);
		j++;
	}
	
	//restore the last cell that we removed to ensure that we still have a unique solution
	commitMove(game,position,targetValue);
}
Exemplo n.º 2
0
int generateRandomBoard(sudokuGrid *game) {
	int position, trialValue, solved;
	int trialValues[NUM_VALUES];
	int i,j;
	
	//if the board is full, we are done
	if (isBoardFull(game)) {
		solved = TRUE;
	} else {
		//populate and then shuffle array of trial values
		j = 0;
		for (i = MIN_VALUE; i <= MAX_VALUE; i++) {
			trialValues[j] = i;
			j++;
		}
		shuffleArr(trialValues,NUM_VALUES);
		
		solved = FALSE;
		position = getEmptyCell(game);
		j = 0;
		
		while (!solved && (j < NUM_VALUES)) {
			trialValue = trialValues[j];
			
			if (isLegalMove(game,position,trialValue)) {
				commitMove(game,position,trialValue);
				
				if (generateRandomBoard(game)) {
					//game with trial value in this position is solvable
					solved = TRUE;
				} else {
					//reset to a blank value to try another solution
					commitMove(game,position,BLANK_VALUE);
				}
			}
			
			j++;
		}
	}
	
	return solved;
}
Exemplo n.º 3
0
		void firstSearchTest()
		{
			int aheads; int length;
			cin >> aheads >> length;
			vector<vector<int> > originalBoard(HEIGHT, vector<int>(WIDTH, 0));

			while (true)
			{
				originalBoard = generateRandomBoard();
				//inputBoard(originalBoard);
				debug(originalBoard);

				for (int l = 0; l <= aheads; l++)
				{
					vector<vector<int>> board = originalBoard;
					Route ansRoute;
					int evalMAX = -1;
					int size = 1 << 20;
					int row = 0, column = 0;

					for (int i = 0; i < HEIGHT; i++)
					{
						for (int j = 0; j < WIDTH; j++)
						{
							vector<vector<int>> tmpBoard = board;
							int evalNum;

							Route route = firstSearch_4(board, l, length, i, j, evalNum);

							//if ((i == 2 && j == 4) || (i == 2 && j == 1))
							//{
							//	debug(tmpBoard);
							//}
							Route simpliFiedRoute;
							simplifyRoutePerfectly(route, i, j, simpliFiedRoute);

							//if ((i == 2 && j == 4) || (i == 2 && j == 1))
							//{
							//	cout << endl;
							//	cout << "debug:" << endl;
							//	debug(board);
							//	debug(tmpBoard);
							//	cout << i << ", " << j << endl;
							//	debug(route);
							//	cout << "↓" << endl;
							//	debug(simpliFiedRoute);
							//	cout << evalNum << endl;
							//	cout << endl;
							//}

							//Route simpliFiedRoute = route;
							if (evalMAX < evalNum)
							{
								ansRoute = simpliFiedRoute;
								evalMAX = evalNum;
								row = i;
								column = j;
								size = ansRoute.size();
							}
							else if (evalMAX == evalNum && size < simpliFiedRoute.size())
							{
								ansRoute = simpliFiedRoute;
								evalMAX = evalNum;
								row = i;
								column = j;
								size = ansRoute.size();
							}
						}
					}

					//debug(ansRoute);
					//cout << row << ", " << column << endl;
					moveByRoute(board, ansRoute, row, column);
					//debug(board);

					int evalNum = evalProcess(board);
					cout << "Aheads:       " << l << endl;
					cout << "Evaluation:   " << evalNum << endl;
					evalNum += fillBoardRandom(board) * 100;//毎回ボード結果が変わるので平等でない
					cout << "RandomFilled: " << evalNum / 100 * 100 << endl;

				}
				string tmp;
				std::getline(cin, tmp);
			}

		}