diff --git a/all.c b/all.c index aebb7a8..aa824e9 100644 --- a/all.c +++ b/all.c @@ -2,40 +2,18 @@ #include #include #define range9(x) for(int x = 0; x < 9; x ++) - +#define range81() for(int i = 0; i < 9; i ++) for(int j = 0; j < 9; j ++) struct Sudoku{ int inc[9][9]; }; #define MAXSIZE 1000 -struct Sudoku *M_PtrReg[MAXSIZE]; -int PRtp = 0; -void ptrInit(){ - for(int i = 0; i < MAXSIZE; i ++) - M_PtrReg[i] = (struct Sudoku *)malloc(sizeof(struct Sudoku)); -} -void ptrClear(){ - for(int i = 0; i < MAXSIZE; i ++) - free(M_PtrReg[i]); -} -struct Sudoku *ptrReg(){return M_PtrReg[PRtp++];} -void ptrFree(){--PRtp;} +struct Sudoku M_Sudoku[MAXSIZE]; +int tp=0; -void sudokuClear(struct Sudoku *a){ - range9(i) - range9(j) - a->inc[i][j] = 0; -} -void sudokuCopy(struct Sudoku src, struct Sudoku *dst){ - range9(i) - range9(j) - dst->inc[i][j] = src.inc[i][j]; -} -void sudokufromArray(int src[9][9], struct Sudoku *dst){ - range9(i) - range9(j) - dst->inc[i][j] = src[i][j]; -} +void sudokuClear(struct Sudoku *a){range81()a->inc[i][j] = 0;} +void sudokuCopy(struct Sudoku src, struct Sudoku *dst){range81()dst->inc[i][j] = src.inc[i][j];} +void sudokufromArray(int src[9][9], struct Sudoku *dst){range81()dst->inc[i][j] = src[i][j];} void sudokuPrint(struct Sudoku a){ range9(i){ if(!(i % 3)) putchar('\n'); @@ -54,19 +32,18 @@ void sudokuPrint(struct Sudoku a){ void initRandomSeed(){srand(time(NULL));} struct Sudoku sudokuRandomGenerate(unsigned int fill){// % fill %= 101; - struct Sudoku *res = ptrReg(); - range9(i) - range9(j) - if(rand()%100 <= fill) - res->inc[i][j] = rand() % 9 + 1; - else - res->inc[i][j] = 0; + struct Sudoku *res = M_Sudoku + tp++; + range81() + if(rand()%100 <= fill) + res->inc[i][j] = rand() % 9 + 1; + else + res->inc[i][j] = 0; return *res; } struct Sudoku tags; int tag(struct Sudoku *a, int i, int j, int x, int y, struct Sudoku *tags){ if(i == x && j == y) return 0; - if(a->inc[i][j] == a->inc[x][y]){ + if(a->inc[i][j] && a->inc[i][j] == a->inc[x][y]){ printf("False:repetition found in (%d,%d) and (%d,%d) with the number {%d}\n", i+1, j+1, x+1, y+1, a->inc[x][y]); return 1; } @@ -80,13 +57,11 @@ int tagPoint(struct Sudoku *a, int i, int j, struct Sudoku *tags){ return 0; } int tagsInit(struct Sudoku *a, struct Sudoku *tags){ - range9(i) - range9(j) - tags->inc[i][j] |= (1 << a->inc[i][j]); - range9(i) - range9(j) - if(a->inc[i][j] && tagPoint(a, i, j, tags)) - return 1; + range81() + tags->inc[i][j] |= (1 << a->inc[i][j]); + range81() + if(a->inc[i][j] && tagPoint(a, i, j, tags)) + return 1; } int sudokuJudge(struct Sudoku a){ struct Sudoku tags; @@ -97,45 +72,43 @@ int sudokuJudge(struct Sudoku a){ } void tagFindLeast(struct Sudoku *a, int *x, int *y, struct Sudoku *tags){ int minv = 10; - int mini=0, minj=0; - range9(i) - range9(j) - if(!a->inc[i][j]){ - int cnt = 0; - range9(k) - cnt += (tags->inc[i][j] >> k) & 1; - if(cnt < minv && cnt != 0){ - minv = cnt; - mini = i; - minj = j; - } - } - *x = mini; - *y = minj; + *x = *y = 0; + range81() + if(!(a->inc[i][j])){ + int cnt = 0; + range9(k) + cnt += !((tags->inc[i][j] >> (k+1)) & 1); + if(cnt < minv && cnt != 0) + minv = cnt, + *x = i, + *y = j; + } + } struct Sudoku *sudokuFill__(struct Sudoku *a, int unfillednum, struct Sudoku *tags){ struct Sudoku *pres, *t_tags; if(!unfillednum){ - pres = ptrReg(); + pres = M_Sudoku + tp ++; sudokuCopy(*a, pres); - sudokuPrint(*tags); + sudokuPrint(*a); return pres; } int m=0, n=0; tagFindLeast(a, &m, &n, tags); range9(i) - if(!((tags->inc[m][n] >> i) & 1)){ - a->inc[m][n] = i; - t_tags = ptrReg(); + if(!((tags->inc[m][n] >> (i+1)) & 1)){ + t_tags = M_Sudoku + tp ++; sudokuCopy(*tags, t_tags); + a->inc[m][n] = i+1; tagPoint(a, m, n, t_tags); + // printf("step:%d:[%d %d]:%d\n", unfillednum, m+1, n+1, i+1); // sudokuPrint(*a); // getchar(); pres = sudokuFill__(a, unfillednum - 1, t_tags); if(pres) return pres; a->inc[m][n] = 0; - ptrFree(); + tp --; } return NULL; } @@ -144,33 +117,39 @@ struct Sudoku *sudokuFill(struct Sudoku *a){ sudokuClear(&tags); if(tagsInit(a, &tags)) return NULL; int unfillednum = 0; - range9(i) - range9(j) - if(!a->inc[i][j]) - unfillednum ++; - printf("%d", unfillednum); + range81() + if(!a->inc[i][j]) + unfillednum ++; return sudokuFill__(a, unfillednum, &tags); } int main(){ - ptrInit(); + clock_t st, end; + st=clock(); initRandomSeed(); + int board[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9}}; struct Sudoku a; - sudokuCopy(sudokuRandomGenerate(10), &a); + sudokufromArray(board, &a); printf("The original Sudoku matrix: \n"); sudokuPrint(a); if(sudokuJudge(a)){ - printf("No solution!\n"); - ptrClear(); + printf("No solution!\n"); return 0; } struct Sudoku *pres = sudokuFill(&a); if(!pres){ - printf("No solution!\n"); - ptrClear(); + printf("No solution!\n"); return 0; } printf("The solution of Sudoku matrix:\n"); sudokuPrint(*pres); - printf("%d", PRtp); - ptrClear(); + end=clock(); + printf("dur:%ldms\n", end-st); } \ No newline at end of file