-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkk1.cpp
executable file
·263 lines (203 loc) · 5.08 KB
/
kk1.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
KenKen Solver with Backtracking
@author: Christian Idylle
@date: 09/29/11
@description: Authored for CSCI561 USC
*/
#include <iostream>
#include <fstream>
using namespace std;
typedef struct cell {
int cage; //what cage/block the cell belongs to. User defined.
int cageSize; //number of cells in a block/cage.
int target; //the target value of the block/cage.
char operand; //the operation used in the block/cage.
int value; //the set of possible values assigned to the cell.
} cell;
cell * X;
int n;
void backtrack(int index);
void print_solution();
bool check_constraints(int index,int candidate);
bool check_row(int index,int candidate);
bool check_col(int index,int candidate);
bool check_cage(int index,int candidate);
bool check_add(int index,int candidate);
bool check_sub(int index,int candidate);
bool check_multiply(int index,int candidate);
bool check_div(int index,int candidate);
int max(int a, int b);
int min(int a, int b);
int main() {
char filename[50];
cout<<"Welcome to CSCI561 Programming Assignment 1" <<endl;
cout<<"KenKen Solve using backtracking by @Christian Idylle"<<endl;
cout<<" "<<endl;
cout << "Enter n (For a board of size nxn): ";
cin >> n;
cout << "Enter filename of board inputs (eg. kkboard1.txt): ";
cin >> filename;
//Create the array of cells and initializing values.
X = new cell[n*n];
// Reading txt file to construct the board.
// format: one line per cell: cage#, cageSize, target value, operator.
ifstream in(filename);
if(!in) { cout << "Cannot open file! \n"; return 1; }
for(int i=0;i<(n*n);i++)
{
in >> X[i].cage;
in >> X[i].cageSize;
in >> X[i].target;
in >> X[i].operand;
}
in.close();
//Launching backtracking to find solutions
int index=0;
backtrack(index);
//clean up
delete [] X;
return 0;
}
void backtrack(int index)
{
if(index==(n*n))
{print_solution();}
else
{
for(int i=1;i<=n;i++)
{
int candidate = i;
if(check_constraints(index,candidate))
{
X[index].value=candidate;
backtrack(index+1);
}
}
}
}
bool check_constraints(int index, int candidate)
{
if(!check_row(index,candidate)) return false;
if(!check_col(index,candidate)) return false;
if(!check_cage(index,candidate)) return false;
return true;
}
bool check_cage(int index, int candidate)
{
int checked_cells=0;
for(int i=0;i<=index;i++)
{
if(X[i].cage==X[index].cage) checked_cells++;
}
if(checked_cells==X[index].cageSize) {
switch(X[index].operand)
{
case '=':
if(candidate == X[index].target)
{return true; break;}
else {return false;break;}
case '+':
if(check_add(index,candidate))
{return true; break;}
else {return false; break;}
case '-':
if(check_sub(index,candidate))
{return true;break;}
else {return false; break;}
case '*':
if(check_multiply(index,candidate))
{return true; break;}
else {return false; break;}
case '/':
if(check_div(index,candidate))
{return true; break;}
else{return false; break;}
}
}
else {return true;}
}
bool check_row(int index,int candidate)
{
int row = int(index/n);
for(int i=(n*row);i<(n*row)+(index%n);i++)
{
if(X[i].value==candidate&&i!=index) return false;
}
return true;
}
bool check_col(int index,int candidate)
{
int col = index%n;
for(int i=col;i<index;i=i+n)
{
if(X[i].value==candidate&&i!=index) return false;
}
return true;
}
bool check_add(int index, int candidate)
{
//checks to see if all the added values of a cage add up correctly.
int sum=candidate;
for(int i=0;i<index;i++)
{
if(X[i].cage==X[index].cage) sum+=X[i].value;
}
if(sum!=X[index].target) return false;
else return true;
}
bool check_sub(int index,int candidate)
{
//checks in the substraction between the 2 cells in the block make the target value. The min value should always be substracted to max value.
int compared_value;
for(int i=0;i<index;i++)
{
if(X[i].cage==X[index].cage) compared_value=X[i].value;
}
if((max(compared_value,candidate) - min(compared_value,candidate)) != X[index].target) return false;
else return true;
}
bool check_multiply(int index,int candidate)
{
//Same thing as add, except multiplication is used. 2+ cells possible per cage.
int product=candidate;
for(int i=0;i<index;i++)
{
if(X[i].cage==X[index].cage) product*=X[i].value;
}
if(product!=X[index].target) return false;
else return true;
}
bool check_div(int index, int candidate)
{
//Should check that the div between the max value and the min value of the two cells in the div cage make the target value.
//Only 2 cells possible per cage.
int compared_value;
for(int i=0;i<index;i++)
{
if(X[i].cage==X[index].cage) compared_value=X[i].value;
}
if((max(compared_value,candidate)/min(compared_value,candidate)) != X[index].target) return false;
else return true;
}
int max(int a, int b)
{
if(a>b) return a;
else return b;
}
int min(int a, int b)
{
if(a<b) return a;
else return b;
}
void print_solution()
{
for(int i = 0; i<n; i++)
{
for(int j=i*n;j<(i*n)+n;j++)
{
cout<<"{"<< X[j].value << "}";
}
cout<<endl;
}
cout << " " << endl;
}