阅读背景:

数独求解----回溯递归,无脑枚举版

来源:互联网 
 
 
 
 
#include <stdlib.h>#include <stdio.h>#define LENGTH 9int answer = 0;void printSudo(int array[][LENGTH]) {    printf("\n\n");    int i, j;    for (i = 0; i < LENGTH; i++) {        if ((i + 1) % 3 == 0)            printf("\n\n");        for (j = 0; j< LENGTH; j++) {            if ((j + 1) % 3 == 0)                printf("\t");            printf("%d  ", array[i][j]);        }        printf("\n");    }    exit(0);}void initSudoArray(int array[][LENGTH]) {    int i, j;    FILE *fp;    if ((fp = fopen("sudo_input", "r")) == NULL) {        printf("File open failed !\n");        exit(-1);    }    for (i = 0; i < LENGTH; i++) {        for (j = 0; j < LENGTH; j++) {            fscanf(fp, "%d", &array[i][j]);        }    }        fclose(fp);}int checkSudo(int array[][LENGTH], int i, int j, int testVal) {    int row, col;    printf("checkSudo for [%d][%d] testVal = %d\n", i, j, testVal);    // fixed to col j, check for the rows    for (row = 0; row < LENGTH; row++) {        printf("check for rows! [%d][%d]  = %d\n", row, j, array[row][j]);        if (array[row][j] == testVal)            return 0;    }    // fixed to row i, check for cols    for (col = 0; col < LENGTH; col++) {        printf("check for cols! [%d][%d] = %d\n", i, col, array[i][col]);        if (array[i][col] == testVal)            return 0;    }    //check for the sub-square    int row_subSquare = (i / 3) * 3;    int col_subSquare = (j / 3) * 3;    printf("[%d][%d]\n", row, col);    for (row = row_subSquare; row < row_subSquare + 3; row++) {        for (col = col_subSquare; col < col_subSquare + 3; col++) {            printf("check for sub-square! [%d][%d] = %d\n", row, col, array[row][col]);            if (array[row][col] == testVal)                return 0;        }    }    return 1;    }// length is the plane index of the sudo arrayvoid sudo_solve(int array[][LENGTH], int length) {    // i for rows, j for cols    int i, j;    int testVal;    int tempArray[LENGTH][LENGTH];    //dump the array to tempArray    for (i = 0; i < LENGTH; i++) {        for (j = 0; j < LENGTH; j++)            tempArray[i][j] = array[i][j];    }    i = length / LENGTH;    j = length % LENGTH;    printf("array[%d][%d] = %d", i, j, array[i][j]);    if (array[i][j] != 0) {    // there is a val in the slot array[i][j]        if (length == 80)            printSudo(tempArray);        else            sudo_solve(tempArray, length + 1);    } else {    // there is no val in the slot array[i][j]        for (testVal = 1; testVal <= LENGTH; testVal++) {            if (checkSudo(tempArray, i, j, testVal) != 0) {                tempArray[i][j] = testVal;                if (length == 80)                    printSudo(tempArray);                else                     sudo_solve(tempArray, length + 1);                                tempArray[i][j] = 0;            }        }    }}int main(void) {    int array[LENGTH][LENGTH];    initSudoArray(array);    sudo_solve(array, 0);    if (answer == 0)        printf("There is no answer for this sudo!");    return 0; }#include <stdlib.h>#include <stdio.h>#def



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: