-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastMagicSquareGenerator.cpp
213 lines (182 loc) · 4.77 KB
/
fastMagicSquareGenerator.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
/* C++ program to generate all possible MagicSquare solutions using backtracking */
#include<iostream>
#include<vector>
using namespace std;
void printMagicSquare(vector < vector <int> > &sol) {
cout << "Found a Solution !" << endl;
for (vector< vector<int> >::iterator row=sol.begin(); row != sol.end(); row++) {
for (vector<int>::iterator col=row->begin(); col != row->end(); col++) {
cout << *col << " ";
}
cout <<endl;
}
cout <<endl;
}
bool checkMagicSquare(vector < vector <int> > &sol, int n) {
int magicNumber = (n * ((n*n) + 1))/2;
int diagonal1Sum = 0;
int diagonal2Sum = 0;
for (int i=0; i < n; i++) {
int rowSum = 0;
int colSum = 0;
diagonal1Sum += sol[i][i];
diagonal2Sum += sol[i][n-1-i];
for (int j=0; j < n; j++) {
rowSum+= sol[i][j];
colSum+= sol[j][i];
}
if ((rowSum != magicNumber) || (colSum != magicNumber))
return false;
}
if ((diagonal1Sum != magicNumber) || (diagonal2Sum != magicNumber))
return false;
return true;
}
/* Increases efficiency tremendously via early detection and forces backtracking */
bool checkMagicSquareSoFar(vector < vector <int> > &sol, int n, int steps) {
bool diagonal1SumRequired = false;
bool diagonal2SumRequired = false;
bool rowSumRequired = false;
bool colSumRequired = false;
int magicNumber = (n * ((n*n) + 1))/2;
int row = steps / n;
int col = steps % n;
int diagonal1Sum = 0;
int diagonal2Sum = 0;
int rowSum = 0;
int colSum = 0;
if ((row == (n-1)) && (col == 0))
diagonal2SumRequired = true;
if ((row == (n-1)) && (col == (n-1)))
diagonal1SumRequired = true;
if (row == (n-1))
rowSumRequired = true;
if (col == (n-1))
colSumRequired = true;
if (diagonal1SumRequired || diagonal2SumRequired || colSumRequired || rowSumRequired) {
for (int i=0; i < n; i++) {
if (diagonal1SumRequired) {
diagonal1Sum += sol[i][i];
}
if (diagonal2SumRequired) {
diagonal2Sum += sol[i][n-1-i];
}
if (colSumRequired) {
colSum+= sol[row][i];
}
if (rowSumRequired) {
rowSum+= sol[i][col];
}
}
if (colSumRequired && colSum != magicNumber) {
return false;
}
if (rowSumRequired && rowSum != magicNumber) {
return false;
}
if (diagonal1SumRequired && diagonal1Sum != magicNumber) {
return false;
}
if (diagonal2SumRequired && diagonal2Sum != magicNumber) {
return false;
}
}
return true;
}
/* Brute force insight, there are n*n cells to fill. Lets step through every assignment
and backtrack the used and assigned values to find all possible solutions */
void generateMagicSquaresUtil(vector < vector <int> > &sol, int steps, int n, bool *used) {
if (steps == (n*n)) {
if (checkMagicSquare(sol, n)) {
printMagicSquare(sol);
}
return;
}
for (int i=1; i<=(n*n); i++) {
if (used[i])
continue;
used[i] = true;
sol[(steps/n)][(steps%n)] = i;
generateMagicSquaresUtil(sol, (steps+1), n, used);
used[i] = false;
sol[(steps/n)][(steps%n)] = 0;
}
return;
}
void generateMagicSquaresUtilFaster(vector < vector <int> > &sol, int steps, int n, bool *used) {
if (steps == (n*n)) {
printMagicSquare(sol);
return;
}
for (int i=1; i<=(n*n); i++) {
if (used[i])
continue;
used[i] = true;
sol[(steps/n)][(steps%n)] = i;
if (checkMagicSquareSoFar(sol, n, steps)) {
generateMagicSquaresUtilFaster(sol, (steps+1), n, used);
}
used[i] = false;
sol[(steps/n)][(steps%n)] = 0;
}
return;
}
bool generateMagicSquareUtilFaster(vector < vector <int> > &sol, int steps, int n, bool *used) {
if (steps == (n*n)) {
printMagicSquare(sol);
return true;
}
for (int i=1; i<=(n*n); i++) {
if (used[i])
continue;
used[i] = true;
sol[(steps/n)][(steps%n)] = i;
if (checkMagicSquareSoFar(sol, n, steps) &&
generateMagicSquareUtilFaster(sol, (steps+1), n, used)) {
return true;
}
used[i] = false;
sol[(steps/n)][(steps%n)] = 0;
}
return false;
}
void generateMagicSquare(int n) {
if ((n == 2) || (n <= 0)) {
cout << "No solution possible" << endl;
return;
}
vector < vector <int> > sol(n, vector<int>(n));
bool* used = new bool[(n*n) + 1];
generateMagicSquareUtilFaster(sol, 0, n, used);
delete[] used;
}
void generateMagicSquares(int n) {
if ((n == 2) || (n <= 0)) {
cout << "No solution possible" << endl;
return;
}
vector < vector <int> > sol(n, vector<int>(n));
bool* used = new bool[(n*n) + 1];
// generateMagicSquaresUtil(sol, 0, n, used);
generateMagicSquaresUtilFaster(sol, 0, n, used);
delete[] used;
}
int main(int argc, char *argv[])
{
int n=5;
char c;
#if 0
cout << "Enter magic Square size : " << endl;
cin >> n;
cout << "One possible result might be : " << endl;
#endif
generateMagicSquare(n);
#if 0
cout << "Do you want to generate all possible magic squares ? (Y/N)" << endl;
cin >> c;
if (toupper(c) == 'Y') {
generateMagicSquares(n);
}
#endif
return 0;
}