Compare commits
4 Commits
fcb33c53ba
...
main
Author | SHA1 | Date | |
---|---|---|---|
86ea672173 | |||
97358e6797 | |||
8f387ff781 | |||
370d7516f5 |
78
sudoku.c
78
sudoku.c
@ -14,15 +14,15 @@
|
|||||||
// https://en.wikipedia.org/wiki/Sudoku#/media/File:Sudoku_Puzzle_by_L2G-20050714_standardized_layout.svg
|
// https://en.wikipedia.org/wiki/Sudoku#/media/File:Sudoku_Puzzle_by_L2G-20050714_standardized_layout.svg
|
||||||
/*
|
/*
|
||||||
int sudoku[9][9] = {
|
int sudoku[9][9] = {
|
||||||
{5,3,0,6,0,0,0,9,8},
|
{5,3,VALUE_UNDEFINED,6,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,9,8},
|
||||||
{0,7,0,1,9,5,0,0,0},
|
{VALUE_UNDEFINED,7,VALUE_UNDEFINED,1,9,5,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED},
|
||||||
{0,0,0,0,0,0,0,6,0},
|
{VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,6,VALUE_UNDEFINED},
|
||||||
{8,0,0,4,0,0,7,0,0},
|
{8,VALUE_UNDEFINED,VALUE_UNDEFINED,4,VALUE_UNDEFINED,VALUE_UNDEFINED,7,VALUE_UNDEFINED,VALUE_UNDEFINED},
|
||||||
{0,6,0,8,0,3,0,2,0},
|
{VALUE_UNDEFINED,6,VALUE_UNDEFINED,8,VALUE_UNDEFINED,3,VALUE_UNDEFINED,2,VALUE_UNDEFINED},
|
||||||
{0,0,3,0,0,1,0,0,6},
|
{VALUE_UNDEFINED,VALUE_UNDEFINED,3,VALUE_UNDEFINED,VALUE_UNDEFINED,1,VALUE_UNDEFINED,VALUE_UNDEFINED,6},
|
||||||
{0,6,0,0,0,0,0,0,0},
|
{VALUE_UNDEFINED,6,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED},
|
||||||
{0,0,0,4,1,9,0,8,0},
|
{VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,4,1,9,VALUE_UNDEFINED,8,VALUE_UNDEFINED},
|
||||||
{2,8,0,0,0,5,0,7,9}
|
{2,8,VALUE_UNDEFINED,VALUE_UNDEFINED,VALUE_UNDEFINED,5,VALUE_UNDEFINED,7,9}
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -88,27 +88,6 @@ void printDumpSudoku() {
|
|||||||
printf("+-------+-------+-------+\n");
|
printf("+-------+-------+-------+\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void printDumpQuadrat(int q[]) {
|
|
||||||
for (int row = 0; row < 3; row++) {
|
|
||||||
if (row % 3 == 0) {
|
|
||||||
printf("+-------+\n");
|
|
||||||
}
|
|
||||||
for (int col = 0; col < 3; col++) {
|
|
||||||
if (col % 3 == 0) {
|
|
||||||
if (col != 0) {
|
|
||||||
printf(" ");
|
|
||||||
}
|
|
||||||
printf("|");
|
|
||||||
}
|
|
||||||
|
|
||||||
int f = calculateField(row, col);
|
|
||||||
printf(" %d", q[f]);
|
|
||||||
}
|
|
||||||
printf(" |\n");
|
|
||||||
}
|
|
||||||
printf("+-------+\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// checks if this value is present anywhere in the row
|
// checks if this value is present anywhere in the row
|
||||||
bool isInRow(int row, int val) {
|
bool isInRow(int row, int val) {
|
||||||
for (int col = 0; col < 9; col++) {
|
for (int col = 0; col < 9; col++) {
|
||||||
@ -249,7 +228,39 @@ bool isInQuadrat(int q, int val) {
|
|||||||
void setValue(int q, int f, int val) {
|
void setValue(int q, int f, int val) {
|
||||||
sudoku[q][f] = val;
|
sudoku[q][f] = val;
|
||||||
printf("=> setting %d in Q %d; F %d\n", val, q, f);
|
printf("=> setting %d in Q %d; F %d\n", val, q, f);
|
||||||
printDumpSudoku();
|
//printDumpSudoku();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkSolution() {
|
||||||
|
bool foundError = false;
|
||||||
|
|
||||||
|
for (int val = 1; val <= 9; val++) {
|
||||||
|
// check for val every quadrat
|
||||||
|
for (int q = 0; q < 9; q++) {
|
||||||
|
if (!isInQuadrat(q, val)) {
|
||||||
|
foundError = true;
|
||||||
|
fprintf(stderr, "Value %d not found in Q %d\n", val, q);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for val in every row
|
||||||
|
for (int r = 0; r < 9; r++) {
|
||||||
|
if (!isInRow(r, val)) {
|
||||||
|
foundError = true;
|
||||||
|
fprintf(stderr, "Value %d not found in row %d\n", val, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for val in every col
|
||||||
|
for (int c = 0; c < 9; c++) {
|
||||||
|
if (!isInCol(c, val)) {
|
||||||
|
foundError = true;
|
||||||
|
fprintf(stderr, "Value %d not found in col %d\n", val, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !foundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@ -305,7 +316,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check if only one field is left undefined -> hast to be the one special value
|
// check if only one field is left undefined -> hast to be the one special value
|
||||||
int undefinedFound = VALUE_UNDEFINED;
|
|
||||||
for (int f = 0; f < 9; f++) {
|
for (int f = 0; f < 9; f++) {
|
||||||
// keep in mind that sudoku array might have changed since evaluation
|
// keep in mind that sudoku array might have changed since evaluation
|
||||||
// -> ignore already set values
|
// -> ignore already set values
|
||||||
@ -323,5 +333,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
} while (somethingChanged);
|
} while (somethingChanged);
|
||||||
|
|
||||||
// next step: muss in Feld X sein, weil alle anderen Felder irgendwie anders belegt sein müssen (aber noch nicht sind)
|
printDumpSudoku();
|
||||||
|
|
||||||
|
checkSolution();
|
||||||
}
|
}
|
Reference in New Issue
Block a user