-
Notifications
You must be signed in to change notification settings - Fork 11
/
kattis_sudokunique.cpp
142 lines (131 loc) · 3.64 KB
/
kattis_sudokunique.cpp
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
/**Kattis - sudokunique
* Relatively generic question about solving sudoku. Implementation is slightly tedious. We use the
* heuristic that when doing substitutions, it is preferable to do the one with the least number of
* options. Other than that, it is just brute force with not that much pruning.
*
* Time: O(?), Mem: O(1)
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
bool duplicate;
string sol;
bool winning(string &board)
{
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i * 9 + j] == '0') return false;
}
}
return true;
}
bool validspecific(string &board, int i, int j) // check if the board is valid at a specific cell
{
assert(board[i * 9 + j] != '0');
for (int k = 0; k < 9; k++) {
if (k != j && board[i * 9 + k] == board[i * 9 + j]) return false;
if (k != i && board[k * 9 + j] == board[i * 9 + j]) return false;
}
int bc = (i / 3) * 3;
int br = (j / 3) * 3;
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
if (bc + k != i && br + l != j && board[(bc + k) * 9 + br + l] == board[i * 9 + j])
return false;
}
}
return true;
}
bool valid(string &board)
{ // check if the board is even valid
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i * 9 + j] == '0') continue;
if (!validspecific(board, i, j)) return false;
}
}
return true;
}
void recurse(string board)
{
if (winning(board)) {
if (sol == "") {
sol = board;
return;
}
else {
duplicate = true;
return;
}
}
// we recurse to the place with the least number of options
int mini = 10;
int minx = -1;
int miny = -1;
string minoptions = "";
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i * 9 + j] != '0') continue;
string options = "";
for (int k = 1; k <= 9; k++) {
board[i * 9 + j] = k + '0';
if (validspecific(board, i, j)) {
options += k + '0';
}
board[i * 9 + j] = '0';
}
if ((int)options.length() < mini) {
mini = options.length();
minoptions = options;
minx = i;
miny = j;
}
}
}
if (mini == 0) return;
assert(mini != 10);
for (int i = 0; i < (int)minoptions.length(); i++) {
board[minx * 9 + miny] = minoptions[i];
recurse(board);
if (duplicate) return;
board[minx * 9 + miny] = '0';
}
}
int main()
{
string board;
while (cin >> board) {
string s;
for (int i = 0; i < 80; i++) {
cin >> s;
board += s;
}
sol = "";
if (!valid(board)) {
cout << "Find another job" << endl << endl;
continue;
}
duplicate = false;
recurse(board);
if (duplicate) {
cout << "Non-unique" << endl << endl;
continue;
}
if (sol == "") {
cout << "Find another job" << endl << endl;
continue;
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cout << sol[i * 9 + j] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}