-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
84 lines (67 loc) · 1.38 KB
/
main.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
#include "solverUtils.h"
#include <string>
#include <fstream>
#include <sstream>
bool solveSudoku(GRID grid) {
PAIR empty = findEmptyPosition(grid);
if (empty == FULL) {
return isFullGridValid(grid);
}
int row = empty.first;
int col = empty.second;
for (int num = 1; num <= DIM; ++num) {
if (isEntryValid(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) {
return true;
}
grid[row][col] = EMPTY;
}
}
return false;
}
void loadGrid(std::ifstream & input, GRID & grid) {
for (int i = 0; i < DIM; ++i) {
std::string line;
std::getline(input, line);
std::stringstream sstream;
sstream << line;
for (int j = 0; j < DIM; ++j) {
int bufferInt = 0;
sstream >> bufferInt;
grid[i][j] = bufferInt;
}
sstream.str("");
}
}
int main(int argc, char** argv) {
/*
GRID grid = {
{5, 0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 6, 0, 0, 0, 0, 1},
{9, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 5, 8, 0},
{0, 0, 7, 0, 0, 3, 0, 0, 0},
{0, 6, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 9, 5, 0},
{0, 3, 1, 7, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 4, 0, 0}
};
*/
if (argc != 2) {
std::cerr << "USAGE: "
<< argv[0]
<< " <filename>\n";
return 1;
}
std::ifstream input(argv[1]);
GRID grid;
loadGrid(input, grid);
if (solveSudoku(grid)) {
printGrid(grid, std::cout);
}
else {
std::cout << "OOPS\n";
}
return 0;
}