Пример #1
0
void block_groups(Sudoku &sudoku, int group_size, int row_block, int col_block)
{
    HammingWeightIterator subset_iter(group_size);
    int digit_subset;

    // for each set of n numbers S = {k1, k2, ... kn}:
    while ((digit_subset = subset_iter.next()) != -1)
    {
        // C(k) == set of columns containing k
        // C(S) == union of C(k) over k in S
        int blocks_containing = blockcells_containing_any(sudoku, row_block, col_block, digit_subset);

        // if size(C(S)) == n
        if (num_ones(blocks_containing) == group_size)
        {
            // remove all other possibilities from columns in C(S)
            SetBitIndexIterator block_iter(blocks_containing);
            auto end = SetBitIndexIterator::end();

            for (; block_iter != end; ++block_iter)
            {
                int row, col;
                blockunmask(mask(*block_iter), row, col);
                row += row_block * 3;
                col += col_block * 3;
                int prev = sudoku(row, col);
                sudoku.set(row, col, prev & digit_subset);
            }
        }
    }
}
Пример #2
0
void row_groups(Sudoku &sudoku, int group_size, int row)
{
    HammingWeightIterator subset_iter(group_size);
    int digit_subset;

    // for each set of n numbers S = {k1, k2, ... kn}:
    while ((digit_subset = subset_iter.next()) != -1)
    {
        // C(k) == set of columns containing k
        // C(S) == union of C(k) over k in S
        int cols_containing = columns_containing_any(sudoku, row, digit_subset);

        // if size(C(S)) == n
        if (num_ones(cols_containing) == group_size)
        {
            // remove all other possibilities from columns in C(S)
            SetBitIndexIterator col_iter(cols_containing);
            auto end = SetBitIndexIterator::end();

            for (; col_iter != end; ++col_iter)
            {
                int prev = sudoku(row, *col_iter);
                sudoku.set(row, *col_iter, prev & digit_subset);
            }
        }
    }
}
Пример #3
0
void box_row_eliminate(Sudoku &sudoku, int row, int box, int eliminate_mask)
{
	for (int col = box * 3; col < (box + 1) * 3; ++col)
	{
		int cell = sudoku(row, col);
		int newcell = subtract(cell, eliminate_mask);
		sudoku.set(row, col, newcell);
	}
}
Пример #4
0
  static bool solve(Sudoku & s) {
    init_algorithm(s);
	bool valid = r_solve(filter_sudoku);
	if(valid) {
        size_t i =0;
        for(std::vector<int>& f : filter_sudoku) {
            if(f.size()!=1)
                std::cout<< "No solution found!";
            s.set(f.front(), i/table_width,  i%table_width); // Kordináták fel vannak cserélve!! HAHA :D
            ++i;
        }
    }
    return valid;
  }
Пример #5
0
void only_in_row(Sudoku &sudoku, int row)
{
	int cols_who_contain[9];
	sudoku.digit_to_column_map(row, cols_who_contain);

	for (int num = 0; num < 9; ++num)
	{
		int cols = cols_who_contain[num];
		if (1 == num_ones(cols))
		{
			int col = unmask(cols);
			sudoku.set(row, col, mask(num));
		}
	}
}
Пример #6
0
void only_in_block(Sudoku &sudoku, int row_block, int col_block)
{
	int friends_who_contain[9] = { 0 };

	sudoku.digit_to_block_map(row_block, col_block, friends_who_contain);

	int row_base = row_block * 3;
	int col_base = col_block * 3;

	for (int num = 0; num < 9; ++num)
	{
		int friends = friends_who_contain[num];
		if (1 == num_ones(friends))
		{
			int row, col;
			blockunmask(friends, row, col);
			if (sudoku(row_base + row, col_base + col) != mask(num))
			{
				sudoku.set(row_base + row, col_base + col, mask(num));
			}
		}
	}
}