-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_sudoku.cpp
67 lines (61 loc) · 1.33 KB
/
gen_sudoku.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
#include <iostream>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctime>
int gen_sudoku(std::vector<std::vector<int>>& puzzle)
{
srand(time(NULL));
int num_failures = 0;
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j++)
{
std::set<int> avoid;
for (int jp = 0; jp<j; jp++)
avoid.insert(puzzle[i][jp]);
for (int ip = 0; ip<i; ip++)
avoid.insert(puzzle[ip][j]);
int cube_st_i = 3*int(i/3);
int cube_st_j = 3*int(j/3);
for (int c = 0; c < 9; c++)
{
int cube_i = cube_st_i + c/3;
int cube_j = cube_st_j + c%3;
if (cube_i == i && cube_j == j)
break;
avoid.insert(puzzle[cube_i][cube_j]);
}
std::vector<int> allowed;
for (int n = 1; n <= 9; n++)
{
if (avoid.find(n) == avoid.end())
allowed.push_back(n);
}
if (allowed.size() == 0)
{
num_failures++;
i = 0;
j = 0;
continue;
}
puzzle[i][j] = allowed[rand()%allowed.size()];
}
}
return num_failures;
}
int main(int argc, char *argv[])
{
// Generate puzzle
std::vector<std::vector<int>> puzzle(9, std::vector<int>(9));
int num_failures = gen_sudoku(puzzle);
std::cout << "Number of failures: " << num_failures << std::endl;
// Print puzzle
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j++)
std::cout << puzzle[i][j] << " ";
std::cout << std::endl << std::endl;
}
return 0;
}