Skip to content

Instantly share code, notes, and snippets.

@souri
Created May 23, 2013 10:13
Show Gist options
  • Save souri/5635091 to your computer and use it in GitHub Desktop.
Save souri/5635091 to your computer and use it in GitHub Desktop.
Solve sudoku
#include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;
int in[9][9] = {{9, 0, 5, 0, 0, 7, 8, 0, 1},
{0, 0, 6, 5, 0, 0, 3, 0, 0},
{0, 3, 0, 6, 0, 9, 0, 0, 0},
{0, 0, 9, 0, 0, 6, 0, 0, 4},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{8, 0, 0, 1, 0, 0, 7, 0, 0},
{0, 0, 0, 9, 0, 4, 0, 1, 0},
{0, 0, 4, 0, 0, 5, 9, 0, 0},
{6, 0, 2, 7, 0, 0, 5, 0, 8}};
int out[9][9];
int pos[9][9];
bool solve(int d) {
//printf("d=%d\n", d);
if (81 == d) {
return true;
}
unsigned i = d / 9, j = d % 9;
if (in[i][j] > 0) {
return solve(d + 1);
}
for (unsigned k = 1; k <= 9; ++k) {
if (!(pos[i][j] & (1 << k))) {
//printf("k=%d\n", k);
out[i][j] = k;
int m = 1 << k;
vector<pair<int, int> > v;
for (unsigned p = 0; p < 9; ++p) {
if (!(pos[i][p] & m)) {
pos[i][p] |= m;
v.push_back(make_pair(i, p));
}
if (!(pos[p][j] & m)) {
pos[p][j] |= m;
v.push_back(make_pair(p, j));
}
}
unsigned si = i / 3 * 3, sj = j / 3 * 3;
for (unsigned p = 0; p < 3; ++p) {
for (unsigned q = 0; q < 3; ++q) {
if (!(pos[si + p][sj + q])) {
pos[si + p][sj + q] |= m;
v.push_back(make_pair(si + p, sj + q));
}
}
}
if (solve(d + 1)) {
return true;
}
//printf("-------\n");
for (unsigned p = 0; p < v.size(); ++p) {
pos[v[p].first][v[p].second] &= ~m;
}
}
}
return false;
}
int main() {
for (unsigned i = 0; i < 9; ++i) {
for (unsigned j = 0; j < 9; ++j) {
printf("%d, ", in[i][j]);
if (in[i][j] > 0) {
out[i][j] = in[i][j];
int m = 1 << in[i][j];
for (unsigned k = 0; k < 9; ++k) {
pos[i][k] |= m;
pos[k][j] |= m;
}
unsigned si = i/3 * 3, sj = j/3 * 3;
for (unsigned k = 0; k < 3; ++k) {
for (unsigned p = 0; p < 3; ++p) {
pos[si + k][sj + p] |= m;
}
}
}
}
printf("\n");
}
printf("-----------------\n");
if (solve(0)) {
for (unsigned i = 0; i < 9; ++i) {
for (unsigned j = 0; j < 9; ++j) {
printf("%d, ", out[i][j]);
}
printf("\n");
}
}
else {
printf("No solution.");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment