UVa 1461 - Sudoku Extension

链接

传送门

题意

给出一个数独,其中0可以填1到9任意数字,‘e’只能填偶数,‘o’只能填奇数,其余字母相同字母的格子的数字必须相同。输出给出的数独有多少个解。

思路

DLX基础题,注意限制。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cctype>

using namespace std;

const int maxr = 750;
const int maxn = 330;
const int maxnode = 20000;

// 行编号从1开始,列编号为1~n,结点0是表头结点; 结点1~n是各列顶部的虚拟结点
struct DLX {
int n, sz; // 列数,结点总数
int S[maxn]; // 各列结点数

int row[maxnode], col[maxnode]; // 各结点行列编号
int L[maxnode], R[maxnode], U[maxnode], D[maxnode]; // 十字链表

int ansd, ans[maxr]; // 解

void init(int n) { // n是列数
this->n = n;
// 虚拟结点
for(int i = 0 ; i <= n; ++i) {
U[i] = i; D[i] = i; L[i] = i-1, R[i] = i+1;
}
R[n] = 0; L[0] = n;
sz = n + 1;
memset(S, 0, sizeof(S));
}

void addRow(int r, const vector<int>& columns) {
int first = sz;
for(int i = 0; i < columns.size(); ++i) {
int c = columns[i];
L[sz] = sz - 1; R[sz] = sz + 1; D[sz] = c; U[sz] = U[c];
D[U[c]] = sz; U[c] = sz;
row[sz] = r; col[sz] = c;
S[c]++; sz++;
}
R[sz - 1] = first; L[first] = sz - 1;
}

// 顺着链表A,遍历除s外的其他元素
#define FOR(i,A,s) for(int i = A[s]; i != s; i = A[i])

void remove(int c) {
L[R[c]] = L[c];
R[L[c]] = R[c];
FOR(i,D,c)
FOR(j,R,i) { U[D[j]] = U[j]; D[U[j]] = D[j]; --S[col[j]]; }
}

void restore(int c) {
FOR(i,U,c)
FOR(j,L,i) { ++S[col[j]]; U[D[j]] = j; D[U[j]] = j; }
L[R[c]] = c;
R[L[c]] = c;
}

int dfs(int d) {
if (R[0] == 0) {
ansd = d;
return 1;
}
int c = R[0], res = 0;
FOR(i,R,0) if(S[i] < S[c]) c = i;
remove(c);
FOR(i,D,c) {
ans[d] = row[i];
FOR(j,R,i) remove(col[j]);
res += dfs(d + 1);
FOR(j,L,i) restore(col[j]);
}
restore(c);
return res;
}

bool solve(vector<int>& v) {
v.clear();
if(!dfs(0)) return false;
for(int i = 0; i < ansd; i++) v.push_back(ans[i]);
return true;
}

} solver;

const int SLOT = 0;
const int ROW = 1;
const int COL = 2;
const int SUB = 3;

int encode(int a, int b, int c) {
return a * 81 + b * 9 + c + 1;
}

void decode(int code, int& a, int& b, int& c) {
--code;
c = code % 9; code /= 9;
b = code % 9; code /= 9;
a = code;
}

typedef pair<int, int> pii;

char s[15][15];
vector<pii> a[27];

int main() {
int t;
scanf("%d", &t);
while (t--) {
for (int i = 0; i < 9; ++i) {
scanf("%s", s[i]);
}
fill(a, a + 26, vector<pii>());
solver.init(324);
for (int r = 0; r < 9; ++r) {
for (int c = 0; c < 9; ++c) {
if (isdigit(s[r][c])) {
for (int v = 0; v < 9; ++v) {
if (s[r][c] == '0' || s[r][c] == v + '1') {
vector<int> columns;
columns.push_back(encode(SLOT, r, c));
columns.push_back(encode(ROW, r, v));
columns.push_back(encode(COL, c, v));
columns.push_back(encode(SUB, (r / 3) * 3 + c / 3, v));
solver.addRow(encode(r, c, v), columns);
}
}
} else if (s[r][c] == 'e' || s[r][c] == 'o') {
for (int v = s[r][c] == 'e' ? 1 : 0; v < 9; v += 2) {
vector<int> columns;
columns.push_back(encode(SLOT, r, c));
columns.push_back(encode(ROW, r, v));
columns.push_back(encode(COL, c, v));
columns.push_back(encode(SUB, (r / 3) * 3 + c / 3, v));
solver.addRow(encode(r, c, v), columns);
}
} else {
a[s[r][c] - 'a'].push_back(make_pair(r, c));
}
}
}
for (int i = 0; i < 26; ++i) {
if (a[i].empty()) {
continue;
}
int r = 0, c = 0;
for (int v = 0; v < 9; ++v) {
vector<int> columns;
for (int j = 0; j < a[i].size(); ++j) {
r = a[i][j].first, c = a[i][j].second;
columns.push_back(encode(SLOT, r, c));
columns.push_back(encode(ROW, r, v));
columns.push_back(encode(COL, c, v));
columns.push_back(encode(SUB, (r / 3) * 3 + c / 3, v));
}
solver.addRow(encode(r, c, v), columns);
}
}
printf("%d\n", solver.dfs(0));
}
return 0;
}