-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
172 lines (143 loc) · 5.09 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
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
168
169
170
171
172
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include "SudokuState.h"
using namespace std;
void bruteForceSearch(char initstate[9][9]);
SudokuState bruteForceAlgorithm(SudokuState currentState, const int spot[3]);
int main(int argc, char const *argv[])
{
//Empty template to use when creating a new Sudoku problem to be solved
// char initialState[9][9] = {
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'},
// {'-', '-', '-', '-', '-', '-', '-', '-', '-'}};
char initialState[9][9] = {
{'8', '-', '2', '4', '9', '-', '-', '-', '7'},
{'7', '-', '-', '2', '-', '8', '3', '-', '6'},
{'-', '9', '6', '7', '-', '-', '-', '-', '-'},
{'-', '-', '-', '8', '-', '7', '-', '-', '-'},
{'-', '5', '-', '-', '4', '-', '-', '-', '-'},
{'9', '-', '4', '5', '-', '-', '-', '-', '-'},
{'3', '-', '-', '9', '-', '-', '-', '1', '-'},
{'5', '6', '-', '3', '-', '-', '-', '2', '-'},
{'-', '2', '-', '-', '-', '-', '-', '6', '3'}};
bruteForceSearch(initialState);
return 0;
}
void bruteForceSearch(char initstate[9][9])
{
//creates three states from the inital sudoku problem
SudokuState initialState = SudokuState(initstate);
SudokuState state = SudokuState(initstate);
SudokuState solvedState;
/**
* Three different place holders.
* spot[0] = current x position
* spot[1] = current y position
* spot[2] = current x,
*/
int spot[3] = {0, 0, 1};
//Changed is used to see if the value has been changed since the original state
bool changed = false;
/**
* for loop to find the first location location of -
* and sets the x and y location to the location of -
* in the 2D array
*/
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (state.state[i][j] == '-' && changed == false)
{
spot[0] = i;
spot[1] = j;
changed = true;
}
//Begin of brute force. Repeat until solved;
while (solvedState.checkSolved() != -1)
{
char aChar = '0' + spot[2]++;
//Set the current - to a digit, and print it out
state.state[spot[0]][spot[1]] = aChar;
cout << state.print() << endl;
//if solved, sets solvedState equal to current state
if (state.checkSolved() == -1)
solvedState = SudokuState(state.state);
SudokuState newState;
//If solved reponse is greater than 90, entry is valid; start algorithm
if (state.checkSolved() > 90)
newState = bruteForceAlgorithm(state, spot);
//Check if newly generated state was a solved state
if (newState.checkSolved() == -1)
solvedState = SudokuState(newState.state);
//Exit statement if no solutions are found
if (spot[2] == 10 && newState.checkSolved() != -1)
{
cout << "No solution found." << endl;
cout << "Double check input and please report input if error exists." << endl;
exit(2);
}
}
//Generic output when solution is found
cout << "\n\n\nThis is the solved state\n\n";
if (solvedState.checkSolved() == -2)
{
cout << state.print() << endl;
}
else
{
cout << solvedState.printColor(initialState) << endl;
}
}
SudokuState bruteForceAlgorithm(SudokuState currentState, const int spot[3])
{
SudokuState state = currentState;
SudokuState solvedState;
int location[3] = {spot[0], spot[1], 1};
bool changed = false;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (state.state[i][j] == '-' && changed == false)
{
location[0] = i;
location[1] = j;
changed = true;
}
cout << location[0] << ", " << location[1] << endl;
while (state.checkSolved() != -1)
{
char aChar = '0' + location[2]++;
if (location[2] == 11)
return currentState;
SudokuState pureState = state;
state.state[location[0]][location[1]] = aChar;
//Comment this line out for faster speed and to remove delay
//this_thread::sleep_for(chrono::nanoseconds(5000000));
SudokuState newState;
if (state.checkSolved() < 90)
{
if (state.checkSolved() == -1)
{
return state;
}
}
else
{
newState = bruteForceAlgorithm(state, location);
}
if (newState.checkSolved() == -1)
{
solvedState = SudokuState(newState.state);
return solvedState;
}
}
return currentState;
}