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
|
#include <iostream>
using namespace std;
int answersArray[3456][4][4], currentAnswer=0;
//Prototypes
void fillArray(int square[][4]);
void printIt(int num);
int main()
{
int square[4][4];
while (currentAnswer < 3456)
{
fillArray(square);
printIt(currentAnswer - 1);
}
cout << "\nPress enter key to continue...";
cin.get();
return 0;
}
void printIt(int num)
{
for(int i=0; i < 4; i++)
{
for(int j=0; j < 4; j++)
cout << answersArray[num][j][i] << " ";
cout << "\n";
}
cout << "\n";
}
bool inSquare(int val, int square[][4]) //returns true if val is already in square
{
int count;
count = 0;
for(int x=0; x < 4; x++)
for(int y=0; y < 4; y++)
{
if(square[x][y] == val) count++;
if(count > 1) return true;
}
return false;
}
bool checkNumbers(int square[][4])
{
bool getOut;
for(int i=1; i < 16; i++)
{
getOut = true;
for(int x=0; x < 4; x++)
{
for(int y=0; y < 4; y++)
if(square[x][y] == i)
{
getOut = false;
break;
}
if(getOut == false) break;
}
if(getOut == true) return false;
}
return true;
}
bool inAnswersArray(int square[][4])
{
int x, y;
if(currentAnswer == 0) return false;
for(int i=0; i < currentAnswer; i++)
{
x = 0;
y = 0;
while(square[x][y] == answersArray[i][x][y])
{
x++;
if(x == 3)
{
x = 0;
y++;
}
if(y == 4) return true; //if y == 4 and still in loop then
} //it's already in answers array
}
return false;
}
bool goodSquare(int square[][4])
{
int total;
if(!checkNumbers(square)) return false;
if(inAnswersArray(square)) return false;
//Check 34's
/* rows */
for(int x=0; x < 4; x++)
{
total = 0;
for(int y=0; y < 4; y++)
total += square[x][y];
if(total != 34) return false;
}
/* columns */
for(int x=0; x < 4; x++)
{
total = 0;
for(int y=0; y < 4; y++)
total += square[x][y];
if(total != 34) return false;
}
/* quadrants */
total = square[0][0] + square[0][1] + square[1][0] + square[1][1];
if(total != 34) return false;
total = square[2][0] + square[3][0] + square[2][1] + square[3][1];
if(total != 34) return false;
total = square[0][2] + square[1][2] + square[0][3] + square[1][3];
if(total != 34) return false;
total = square[2][2] + square[2][3] + square[3][2] + square[3][3];
if(total != 34) return false;
/* diagonals */
total = square[0][0] + square[1][1] + square[2][2] + square[3][3];
if(total != 34) return false;
total = square[3][0] + square[2][1] + square[1][2] + square[0][3];
if(total != 34) return false;
//End check 34's
//Save to answers Array
for(int i=0; i < 4; i++)
for(int j=0; j < 4; j++)
answersArray[currentAnswer][j][i] = square[j][i];
currentAnswer++;
return true;
}
bool miniCheck(int &fill, int square[][4], int y, bool row)
{
if(row == true)
fill = 34 - square[0][y] - square[1][y] - square[2][y];
else
fill = 34 - square[y][0] - square[y][1] - square[y][2];
if(fill < 1 || fill > 16) return false;
return true;
}
void fillArray(int square[][4])
{
for(square[0][0]=1; square[0][0] < 17; square[0][0]++)
{
for(square[1][0]=1; square[1][0] < 17; square[1][0]++)
{
if(inSquare(square[1][0], square)) continue;
for(square[2][0]=1; square[2][0] < 17; square[2][0]++)
{
if(inSquare(square[2][0], square)) continue;
if(!miniCheck(square[3][0], square, 0, true)) continue;
for(square[0][1]=1; square[0][1] < 17; square[0][1]++)
{
if(inSquare(square[0][1], square)) continue;
for(square[1][1]=1; square[1][1] < 17; square[1][1]++)
{
if(inSquare(square[1][1], square)) continue;
for(square[2][1]=1; square[2][1] < 17; square[2][1]++)
{
if(inSquare(square[2][1], square)) continue;
if(!miniCheck(square[3][1], square, 1, true)) continue;
for(square[0][2]=1; square[0][2] < 17; square[0][2]++)
{
if(inSquare(square[0][2], square)) continue;
for(square[1][2]=1; square[1][2] < 17; square[1][2]++)
{
if(inSquare(square[1][2], square)) continue;
for(square[2][2]=1; square[2][2] < 17; square[2][2]++)
{
if(inSquare(square[2][2], square)) continue;
if(!miniCheck(square[3][2], square, 2, true)) continue;
if(!miniCheck(square[0][3], square, 0, false)) continue;
if(!miniCheck(square[1][3], square, 1, false)) continue;
if(!miniCheck(square[2][3], square, 2, false)) continue;
if(!miniCheck(square[3][3], square, 3, false)) continue;
if(goodSquare(square))
return;
}
}
}
}
}
}
}
}
}
}
|