Skip to content

Commit b038d42

Browse files
committed
sudoku-solver
1 parent cf39ed4 commit b038d42

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

sudoku-solver.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Sudoku Solver
2+
/// exact cover problem
23
#define REP(i, n) for (int i = 0; i < (n); i++)
34

45
class Solution {
56
private:
6-
static const int N = 9, MAXR = N*N*N, MAXC = N*N*4, M = MAXC+1+MAXR*4;
7-
int L[M], R[M], U[M], D[M], col[M], size[M], res[N*N];
8-
pair<int, int> mean[M];
7+
static const int N = 9, MAXC = N*N*4, M = MAXC+1+N*N*N*4;
8+
int L[M], R[M], U[M], D[M], col[M], size[M], mean[M];
99
void cover(int c) {
1010
L[R[c]] = L[c];
1111
R[L[c]] = R[c];
@@ -21,7 +21,7 @@ class Solution {
2121
U[D[j]] = D[U[j]] = j;
2222
L[R[c]] = R[L[c]] = c;
2323
}
24-
void insert(int i, int l, int r, int c) {
24+
void append(int i, int l, int r, int c) {
2525
L[i] = l; R[i] = r;
2626
D[U[i] = U[c]] = i;
2727
U[D[i] = c] = i;
@@ -39,7 +39,7 @@ class Solution {
3939
for (int i = D[c]; i != c; i = D[i]) {
4040
for (int j = R[i]; j != i; j = R[j])
4141
cover(col[j]);
42-
a[mean[i].first/9][mean[i].first%9] = '1'+mean[i].second;
42+
a[mean[i]/N/N][mean[i]/N%N] = '1'+mean[i]%N;
4343
if (dlx(a)) return true;
4444
for (int j = L[i]; j != i; j = L[j])
4545
uncover(col[j]);
@@ -67,11 +67,11 @@ class Solution {
6767
hi = 9;
6868
}
6969
for (int k = lo; k < hi; k++) {
70-
pair<int, int> me = make_pair(i*N+j, k);
71-
insert(cur, cur+3, cur+1, i*N+k); mean[cur] = me;
72-
insert(cur+1, cur, cur+2, N*N+j*N+k); mean[cur+1] = me;
73-
insert(cur+2, cur+1, cur+3, N*N*2+i*N+j); mean[cur+2] = me;
74-
insert(cur+3, cur+2, cur, N*N*3+(i/3*3+j/3)*N+k); mean[cur+3] = me;
70+
append(cur, cur+3, cur+1, i*N+k);
71+
append(cur+1, cur, cur+2, N*N+j*N+k);
72+
append(cur+2, cur+1, cur+3, N*N*2+i*N+j);
73+
append(cur+3, cur+2, cur, N*N*3+(i/3*3+j/3)*N+k);
74+
fill_n(mean+cur, 4, (i*N+j)*N+k);
7575
cur += 4;
7676
}
7777
}

0 commit comments

Comments
 (0)