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
|
#include <iostream>
#include <cstring>
using namespace std;
void uavlRemoval(short grid[][9], short avl[], int &p, int &q, short &r);
void uavlRemoval(short avl[], short &p);
void numInsertion(short grid[][9], short u[], short v[], short &w, short &x, int &y, int &z);
int main() {
short puz[3][9];
cout << "Prepare to input the 3 * 9 (0s for blanks): " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
cin >> puz[i][j];
}
}
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 9; l++) {
cout << puz[k][l] << " | ";
}
cout << endl;
}
short a, b, c, d, e, f;
a = b = c = d = e = f = 9;
short row1[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
short row2[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
short row3[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
short box1[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
short box2[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
short box3[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// "Removes" used numbers from availbility arrays and decrements their corresponding counters each time a value is found.
// This task precedes actually solving the puzzle.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
if (puz[i][j] > 1) {
if (i == 0) {
uavlRemoval(puz, row1, i, j, a);
}
if (i == 1) {
uavlRemoval(puz, row2, i, j, b);
}
if (i == 2) {
uavlRemoval(puz, row3, i, j, c);
}
if (j < 3) {
uavlRemoval(puz, box1, i, j, d);
}
if (j > 2 && j < 6) {
uavlRemoval(puz, box2, i, j, e);
}
if (j > 5 && j < 9) {
uavlRemoval(puz, box3, i, j, f);
}
} else if (puz[i][j] == 1) {
if (i == 0) {
uavlRemoval(row1, a);
}
if (i == 1) {
uavlRemoval(row2, b);
}
if (i == 2) {
uavlRemoval(row3, c);
}
if (j < 3) {
uavlRemoval(box1, d);
}
if (j > 2 && j < 6) {
uavlRemoval(box2, e);
}
if (j > 5 && j < 9) {
uavlRemoval(box3, f);
}
}
}
}
// Problems arise from here
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
while (i == 0 && a > 0) {
if (j <= 2 && d > 0) {
numInsertion(puz, row1, box1, a, d, i, j);
}
if (j > 2 && j < 6 && e > 0) {
numInsertion(puz, row1, box2, a, e, i, j);
}
if (j > 5 && j < 9 && f > 0) {
numInsertion(puz, row1, box3, a, f, i, j);
}
}
while (i == 1 && b > 0) {
if (j <= 2 && d > 0) {
numInsertion(puz, row2, box1, b, d, i, j);
}
if (j > 2 && j < 6 && e > 0) {
numInsertion(puz, row2, box2, b, e, i, j);
}
if (j > 5 && j < 9 && f > 0) {
numInsertion(puz, row2, box3, b, f, i, j);
}
}
while (i == 2 && c > 0) {
if (j <= 2 && d > 0) {
numInsertion(puz, row3, box1, c, d, i, j);
}
if (j > 2 && j < 6 && e > 0) {
numInsertion(puz, row3, box2, c, e, i, j);
}
if (j > 5 && j < 9 && f > 0) {
numInsertion(puz, row3, box3, c, f, i, j);
}
}
}
}
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 9; y++) {
cout << puz[x][y] << " | ";
}
cout << endl;
}
return 0;
}
// removes unavailable numbers before puzzle is solved
void uavlRemoval(short grid[][9], short avl[], int &p, int &q, short &r) {
avl[grid[p][q] - 1] = 0;
r--;
}
// removes 1 if unavailable before puzzle solution
void uavlRemoval(short avl[], short &p) {
avl[0] = 0;
p--;
}
// inserts available numbers then removes them from corresponding availability arrays
void numInsertion(short grid[][9], short u[], short v[], short &w, short &x, int &y, int &z) {
if (grid[y][z] == 0) {
for (int h = 0; h < 9; h++) {
if (u[h] == v[h] && u[h] > 0) {
w--;
x--;
grid[y][z] = u[h];
u[h] = v[h] = 0;
}
}
}
}
|