创建我自己的数独回溯alghoritm(C)的问题

时间:2016-08-10 18:20:11

标签: c sudoku

我试图用C创建我自己的数独回溯。程序在求解函数中循环。它进入IsNotInVert和IsNotInHor,然后返回到while循环的开头。 (虽然是!= 81)我是C的初学者,我想我开始时有点难以接受。但无论如何,我希望得到一些帮助。

我尝试在我们现在所在的数组中的位置之后调试程序。但是仍然很难完全遵循该计划正在做的事情,我真的希望你们能给我一些提示和技巧,指出我正确的方向!

这里是代码;

#include <stdio.h>

void print_sudoku(int a[9][9]) {
   int i, j, h;
   /* print the sudoku :D */
   for (i = 0; i < 9; i++) {
      for (j = 0; j < 9; j++) {
         printf("%6i ", a[i][j]);
   }
     printf("|---|---|---|");
   printf("\n");
}
printf("\n");
}

/* The solver, 
A counts till 81, 81 is the last number in the puzzle.
If there are no possibilities anymore failed will turn on.
This means we can hop over a static field back.

*/

void Solver (int puzzle[9][9], int staticField[9][9]) { 
int a;
int row;
int col;
int numb;
int failed;
    while(a != 81) {
    if (!IsStatic(row,col,staticField)) {
        do {
        numb++;
        puzzle[row][col] = numb;
        if (numb == 10) {
            a--;
            puzzle[row][col] = 0;
            failed = 1;
            if(col != 0){
            col--;
            }
            else {
                col = 8;
                row--;
            }
        }
        }while(!IsSafe(row,col,puzzle));
            if (col == 9) {
                col = 0;
                if (row != 9) {
                row++;
                } 
            }
        } else if (failed == 0) { a++; }
        else { a--; Solver(puzzle, staticField);}
    } 
    print_sudoku(puzzle);
}

/* If a Number is "static" we are not allowed to change it.
We compare our coordinates with the staticfield "mask" to determine if 
we have a static number.

*/
int IsStatic (int a, int b, int staticField[9][9]) {    
    if (staticField[a][b] == 1 ) {
        return 1;
        }
    else return 0;
}

/* Function who looks if it is allowed to place a number
*/

int IsSafe (int a, int b, int field[9][9]) {
    if (IsNotInVert(a, b, field)) {
        if(IsNotInHor(a, b, field)) {
            if(IsNotInBox(WhichBox(a,b),a,b,field)) {
            return 1;
            } else return 0;
        } else return 0;
    } else return 0;
}


/* 
Function to check if the number we are testing is already in the 3 x 3 box
the if statements (a!=c && b!=d) are to filter yourself out
c= rownumber we want to check
d= columnnumber we want to check
box = array with the numbers in the 3 x 3 box
*/

int IsNotInBox (int coord[1], int c, int d, int field[9][9]) {
    int a = coord[0];
    int b = coord[1];
    int *box[8];

    /* Fill box[] with the numbers in the 3 x 3 box */
    if (a != c && b != d) { box[0] = field[a][b]; }
a++;
    if (a != c && b != d) { box[1] = field[a][b]; }
a++;
    if (a != c && b != d) { box[2] = field[a][b]; }
a - 2;
b++;
    if (a != c && b != d) { box[3] = field[a][b]; }
a++;
    if (a != c && b != d) { box[4] = field[a][b]; }
a++;
    if (a != c && b != d) { box[5] = field[a][b]; }
a - 2;
b++;
    if (a != c && b != d) { box[6] = field[a][b]; } 
a++;
    if (a != c && b != d) { box[7] = field[a][b]; }
a++;
    if (a != c && b != d) { box[8] = field[a][b]; }
    int f;
    for (f = 0; f < 9; f++) {
    if (box[f] == field[c][d]) { return 0; }
    }
    return 1;
}


/* 
To calculate in which 3x3 box you are,

gives back a array with the coordinates of the LEFTCORNER of 
the 3x3 block (4,4 becomes 3,3 8,2 becomes 6,0)

*/

int WhichBox (int a, int b) {
    while (a != 0 || a != 3 || a != 6) { // Make 0,3 or 6
        a--;
    }
    while (b != 0 || b != 3 || b != 6) { // Make 0,3 or 6
        b--;
    }

    int pos[1]; // creating and filling the array
    pos[0] = a;
    pos[1] = b;
    return pos;
}

/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOT WORKING NOT IMPLEMENTED
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

int IsSolved (int a) {
    if (IsNotInVert(8,  8)) {
        if(IsNotInHor(8, 8)) {
            if(IsNotInBox(WhichBox(8,8),8,8)) {
            return 1;
            } else return 0;
        } else return 0;
    } else return 0;
} 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOT WORKING NOT IMPLEMENTED
*/
/*
A is row
B is counter
D is column
~ Same as IsNotInHor
*/

int IsNotInVert (int a, int d, int field[9][9]) {

    int b = 0; // set counter to zero
    for ( b = 0; b < 9; b++) { // For loop till 9
        if (field[d][b] == field[d][a]) { // Check if number is same
            if (a != b ) { // Check if not checking yourself
            return 0;
            }
        }
    } 
    return 1;
}

/*
A = Column
B = Counter
D = Row
*/
int IsNotInHor (int a, int d, int field[8][8]) {

    int b = 0; // set counter to zero
    for ( b = 0; b < 9; b++ ) { // For loop counting till 9 
        if (field[b][a] == field[d][a]) { // Check if number already exist
            if(b != d) { // Check if not checking yourself 
                return 0;
            }
        }
    }
        return 1;
}

/*
Main

*/

int main (void) {
    /* Field with the unsolved puzzle */
    int field[9][9] = {
/*       0  1  2  3  4  5  6  7  8              */
/* 0 */ {0, 0, 7, 0, 0, 5, 0, 1, 0} ,
/* 1 */ {0, 1, 2, 0, 0, 0, 3, 0, 9} ,
/* 2 */ {6, 9, 0, 0, 0, 0, 0, 8, 0} ,
/* 3 */ {0, 0, 0, 9, 0, 3, 0, 0, 7} ,
/* 4 */ {0, 0, 0, 0, 1, 0, 0, 0, 0} ,
/* 5 */ {4, 0, 0, 2, 0, 8, 0, 0, 0} ,
/* 6 */ {0, 7, 0, 0, 0, 0, 0, 4, 1} ,
/* 7 */ {5, 0, 1, 0, 0, 0, 8, 3, 0} ,
/* 8 */ {0, 8, 0, 1, 0, 0, 6, 0, 0} 
    };

    /* For IsStatic to see where static numbers are */
        int staticField[9][9] = {
/*       0  1  2  3  4  5  6  7  8              */
/* 0 */ {0, 0, 1, 0, 0, 1, 0, 1, 0} ,
/* 1 */ {0, 1, 1, 0, 0, 0, 1, 0, 1} ,
/* 2 */ {1, 1, 0, 0, 0, 0, 0, 1, 0} ,
/* 3 */ {0, 0, 0, 1, 0, 1, 0, 0, 1} ,
/* 4 */ {0, 0, 0, 0, 1, 0, 0, 0, 0} ,
/* 5 */ {1, 0, 0, 1, 0, 1, 0, 0, 0} ,
/* 6 */ {0, 1, 0, 0, 0, 0, 0, 1, 1} ,
/* 7 */ {1, 0, 1, 0, 0, 0, 1, 1, 0} ,
/* 8 */ {0, 1, 0, 1, 0, 0, 1, 0, 0} 
    };
print_sudoku(field);
Solver(field, staticField); 

} 

0 个答案:

没有答案