Code:#include <fstream.h> #include <iomanip.h> #include <iostream.h> #define UNDEFINED 17 /* Format: Rows, columns, possibilities. */ unsigned short int array[16][16][18]; bool IsPossibility(unsigned short int, unsigned short int, unsigned short int); void Load(); void CheckPossibility(); void DisplayChallenge(); void Step(); int main() { char selection; Load(); do { cout << "? "; cin >> selection; switch (selection) { case 'r' : Step(); break; case 'c' : Load(); break; case 'd' : DisplayChallenge(); break; case 'p' : CheckPossibility(); } } while (selection != 'e'); return 0; } bool IsPossibility(unsigned short int column, unsigned short int row, unsigned short int test) { for (unsigned short int i = 4 * (column / 4); i < 4 * (column / 4) + 4; ++i) for (unsigned short int j = 4 * (row / 4); j < 4 * (row / 4) + 4; ++j) if (array[j][i][0] == test) return false; if (column == row) for (unsigned short int k = 0; k <= 15; ++k) if (array[k][k][0] == test) return false; else if (column == 15 - row) for (unsigned short int k = 0; k <= 15; ++k) if (array[k][15 - k][0] == test) return false; for (unsigned short int l = 0; l <= 16; ++l) if (array[l][column][0] == test || array[row][l][0] == test) return false; return true; } void CheckPossibility() { char letter; unsigned short int number; cin >> letter >> number; letter -= 'A'; --number; if (array[number][letter][0] != UNDEFINED) cout << setw(4) << array[number][letter][0]; else for (unsigned short int i = 1; i <= 16; ++i) if (IsPossibility(letter, number, i)) cout << setw(4) << i; cout << endl; } void DisplayChallenge() { cout << setw(4) << " "; for (unsigned short int i = 0; i <= 16; ++i) { for (unsigned short int j = 0; j <= 16; ++j) if (i == 0) cout << setw(4) << char(j + 65); else if (j == 0) cout << setw(4) << i; else if (array[i - 1][j - 1][0] == UNDEFINED) cout << setw(4) << " "; else cout << setw(4) << array[i - 1][j - 1][0]; cout << endl; } } void Load() { ifstream fin("Puzzle.txt"); for (int i = 0; i <= 15; ++i) for (int j = 0; j <= 15; ++j) fin >> array[i][j][0]; fin.close(); } void Step() { for (int i = 0; i <= 15; ++i) for (int j = 0; j <= 15; ++j) if (array[i][j][0] == UNDEFINED) { int numFound = 0; for (int k = 0; k <= 16; ++k) if (IsPossibility(j,i,k)) { array[i][j][k + 1] = 1; ++numFound; } if (numFound == 1) for (int l = 0; l <= 16; l++) if (array[i][j][l + 1] == 1) array[i][j][0] = l; } }The idea is to make a program that will solve for the remaining values (the 17s) with numbers 1 - 16. Each 4x4 square of numbers has each number appearing in it. Each column and row also has all 16 numbers appearing in it. The full diagonals, likewise, have 1-16 taking up all of the room with no repeats.Code:In 'Puzzle.txt': 17 17 17 13 17 4 12 17 6 17 3 17 17 7 17 16 8 17 17 7 17 16 17 11 17 9 17 17 17 10 4 6 1 5 17 17 7 6 17 17 13 17 12 17 3 9 17 17 17 12 4 17 17 17 15 17 17 17 17 16 1 17 2 14 10 13 3 17 17 1 17 14 9 7 17 17 5 17 17 17 17 4 17 17 5 17 13 17 17 17 17 10 15 17 1 11 2 17 8 1 17 3 17 17 14 17 17 12 17 6 17 13 5 6 16 15 9 11 10 12 2 8 13 1 4 14 3 7 9 17 7 17 6 17 17 13 5 17 11 17 17 4 15 17 17 1 17 17 17 2 17 15 10 17 17 6 17 17 7 9 17 17 11 14 1 17 17 16 12 17 17 8 2 17 17 17 16 17 17 5 17 17 11 17 17 2 15 17 13 17 17 12 17 9 17 17 12 15 17 17 11 16 17 17 17 17 14 17 11 17 17 10 17 17 4 3 15 17 1 17 17 17 8 5 17 17 5 4 2 17 17 6 17 17 17 3 11 16 17 17 3 17 2 17 17 17 8 17 17 5 14 17 7 15 17 1
Any ideas on what I may be doing to screw this up? I have a feeling its something stupid.



LinkBack URL
About LinkBacks


