c++ - Allow User To Input Data For A Sudoku Solving Program? -
i trying allow user input values sudoku solver keep getting error. here code:
#include <iostream> // #include <fstream> using namespace std; class sudokuboard; void printb(sudokuboard sb); typedef unsigned int uint; const uint maxval = 9; const uint l = 9; const uint c = 9; const uint s = l * c; const uint zonel = 3; const uint zonec = 3; const uint zones = zonel * zonec; const uint lineelements[l][c] = { { 0, 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} }; const uint columnelements[c][l] = { { 0, 9, 18, 27, 36, 45, 54, 63, 72}, { 1, 10, 19, 28, 37, 46, 55, 64, 73}, { 2, 11, 20, 29, 38, 47, 56, 65, 74}, { 3, 12, 21, 30, 39, 48, 57, 66, 75}, { 4, 13, 22, 31, 40, 49, 58, 67, 76}, { 5, 14, 23, 32, 41, 50, 59, 68, 77}, { 6, 15, 24, 33, 42, 51, 60, 69, 78}, { 7, 16, 25, 34, 43, 52, 61, 70, 79}, { 8, 17, 26, 35, 44, 53, 62, 71, 80} }; const uint zoneelements[s / zones][zones] = { { 0, 1, 2, 9, 10, 11, 18, 19, 20}, { 3, 4, 5, 12, 13, 14, 21, 22, 23}, { 6, 7, 8, 15, 16, 17, 24, 25, 26}, {27, 28, 29, 36, 37, 38, 45, 46, 47}, {30, 31, 32, 39, 40, 41, 48, 49, 50}, {33, 34, 35, 42, 43, 44, 51, 52, 53}, {54, 55, 56, 63, 64, 65, 72, 73, 74}, {57, 58, 59, 66, 67, 68, 75, 76, 77}, {60, 61, 62, 69, 70, 71, 78, 79, 80} }; class sudokuboard { public: sudokuboard() : filledin(0) { (uint i(0); < s; ++i) table[i] = useddigits[i] = 0; } virtual ~sudokuboard() { } int const at(uint l, uint c) { // returns value @ line l , row c if (isvalidpos(l, c)) return table[l * l + c]; else return -1; } void set(uint l, uint c, uint val) { // sets cell @ line l , row c hold value val if (isvalidpos(l, c) && ((0 < val) && (val <= maxval))) { if (table[l * c + c] == 0) ++filledin; table[l * c + c] = val; (uint = 0; < c; ++i) // update lines useddigits[lineelements[l][i]] |= 1<<val; (uint = 0; < l; ++i) // update columns useddigits[columnelements[c][i]] |= 1<<val; int z = findzone(l * c + c); (uint = 0; < zones; ++i) // update columns useddigits[zoneelements[z][i]] |= 1<<val; } } void solve() { try { // speed boost scanandset(); // logic approach gobruteforce(); // brute force approach } catch (int e) { // speed boost } } void scanandset() { int b; bool changed(true); while (changed) { changed = false; (uint i(0); < s; ++i) if (0 == table[i]) // there digit written? if ((b = bitcount(useddigits[i])) == maxval - 1) { // if there's 1 digit can place in cell, int d(1); // find digit while ((useddigits[i] & 1<<d) > 0) ++d; set(i / c, % c, d); // fill in changed = true; // board has been changed step must rerun } else if (bitcount(useddigits[i]) == maxval) throw 666; // speed boost } } void gobruteforce() { int max(-1); // find cell _minimum_ number of posibilities (i.e. 1 largest number of /used/ digits) (uint i(0); < s; ++i) if (table[i] == 0) // there digit written? if ((max == -1) || (bitcount(useddigits[i]) > bitcount(useddigits[max]))) max = i; if (max != -1) { (uint i(1); <= maxval; ++i) // go through each possible digit if ((useddigits[max] & 1<<i) == 0) { // if can placed in cell, sudokuboard temp(*this); // create new board temp.set(max / c, max % c, i); // complete attempt temp.solve(); // solve if (temp.getfilledin() == s) { // if board solved (i.e. number of filled in cells s) (uint j(0); j < s; ++j) // copy board 1 set(j / c, j % c, temp.at(j / c, j % c)); return; // break recursive cascade } } } } uint getfilledin() { return filledin; } private: uint table[s]; uint useddigits[s]; uint filledin; bool const inline isvalidpos(int l, int c) { return ((0 <= l) && (l < (int)l) && (0 <= c) && (c < (int)c)); } uint const inline findzone(uint off) { return ((off / c / zonel) * (c / zonec) + (off % c / zonec)); } uint const inline bitcount(uint x) { uint count(0); (; x; ++count, x &= (x - 1)); return count; } }; void printb(sudokuboard sb) { cout << " ***** sudoku solver diego freitas ***** " << endl; cout << " | ------------------------------- |" << endl; (uint i(0); < s; ++i) { if (i % 3 == 0) cout << " |"; cout << " " << sb.at(i / l, % l); if (i % c == c - 1) { if (i / c % 3 == 2) cout << " |" << endl << " | -------------------------------"; cout << " |" << endl; } } cout << endl; } int main(int argc, char *argv[]) { sudokuboard sb; ifstream fin("sudoku.in"); int aux; (uint i(0); < s; ++i) { fin >> aux; sb.set(i / l, % l, aux); } fin.close(); printb(sb); sb.solve(); printb(sb); system ("pause"); return 0; }
if has alterations code appreciated. making during own time , want idea how works.
Comments
Post a Comment