Update all.h

main
po79yr3at 1 year ago
parent f938b95b0e
commit b1384ff2f6

117
all.h

@ -2,53 +2,28 @@
#include<stdlib.h> #include<stdlib.h>
#include<time.h> #include<time.h>
#define range9(x) for(int x = 0; x < 9; x ++) #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{ struct Sudoku{
int v[9][9]; int inc[9][9];
}; };
#define MAXSIZE 1000 #define MAXSIZE 1000
#define DefautTop 100 struct Sudoku M_Sudoku[MAXSIZE];
struct Sudoku *M_PtrReg[MAXSIZE]; int tp=0;
int PRtp = DefautTop, prtp = 0;
void ptrInit(){
for(int i = 0; i < PRtp; i ++)
M_PtrReg[i] = malloc(sizeof(struct Sudoku));
}
void ptrClear(){
for(int i = 0; i < PRtp; i ++)
free(M_PtrReg[i]);
}
struct Sudoku *ptrReg(){
return prtp < PRtp ? M_PtrReg[prtp++]: (M_PtrReg[prtp++] = malloc(sizeof(struct Sudoku)));
}
void ptrFree(){--prtp;}
void sudokuClear(struct Sudoku *a){ void sudokuClear(struct Sudoku *a){range81()a->inc[i][j] = 0;}
range9(i) void sudokuCopy(struct Sudoku src, struct Sudoku *dst){range81()dst->inc[i][j] = src.inc[i][j];}
range9(j) void sudokufromArray(int src[9][9], struct Sudoku *dst){range81()dst->inc[i][j] = src[i][j];}
a->v[i][j] = 0;
}
void sudokuCopy(struct Sudoku src, struct Sudoku *dst){
range9(i)
range9(j)
dst->v[i][j] = src.v[i][j];
}
void sudokufromArray(int src[9][9], struct Sudoku *dst){
range9(i)
range9(j)
dst->v[i][j] = src[i][j];
}
void sudokuPrint(struct Sudoku a){ void sudokuPrint(struct Sudoku a){
range9(i){ range9(i){
if(!(i % 3)) putchar('\n'); if(!(i % 3)) putchar('\n');
range9(j){ range9(j){
if(!(j % 3)) putchar(' '); if(!(j % 3)) putchar(' ');
if(!a.v[i][j]) if(!a.inc[i][j])
putchar('.'); putchar('.');
else else
// putchar(a.v[i][j] + 48); // putchar(a.inc[i][j] + 48);
printf("%d", a.v[i][j]); printf("%d", a.inc[i][j]);
putchar(' '); putchar(' ');
} }
putchar('\n'); putchar('\n');
@ -57,23 +32,22 @@ void sudokuPrint(struct Sudoku a){
void initRandomSeed(){srand(time(NULL));} void initRandomSeed(){srand(time(NULL));}
struct Sudoku sudokuRandomGenerate(unsigned int fill){// % struct Sudoku sudokuRandomGenerate(unsigned int fill){// %
fill %= 101; fill %= 101;
struct Sudoku *res = ptrReg(); struct Sudoku *res = M_Sudoku + tp++;
range9(i) range81()
range9(j)
if(rand()%100 <= fill) if(rand()%100 <= fill)
res->v[i][j] = rand() % 9 + 1; res->inc[i][j] = rand() % 9 + 1;
else else
res->v[i][j] = 0; res->inc[i][j] = 0;
return *res; return *res;
} }
struct Sudoku tags; struct Sudoku tags;
int tag(struct Sudoku *a, int i, int j, int x, int y, 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(i == x && j == y) return 0;
if(a->v[i][j] == a->v[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->v[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; return 1;
} }
tags->v[x][y] |= (1 << a->v[i][j]); tags->inc[x][y] |= (1 << a->inc[i][j]);
return 0; return 0;
} }
int tagPoint(struct Sudoku *a, int i, int j, struct Sudoku *tags){ int tagPoint(struct Sudoku *a, int i, int j, struct Sudoku *tags){
@ -83,12 +57,10 @@ int tagPoint(struct Sudoku *a, int i, int j, struct Sudoku *tags){
return 0; return 0;
} }
int tagsInit(struct Sudoku *a, struct Sudoku *tags){ int tagsInit(struct Sudoku *a, struct Sudoku *tags){
range9(i) range81()
range9(j) tags->inc[i][j] |= (1 << a->inc[i][j]);
tags->v[i][j] |= (1 << a->v[i][j]); range81()
range9(i) if(a->inc[i][j] && tagPoint(a, i, j, tags))
range9(j)
if(a->v[i][j] && tagPoint(a, i, j, tags))
return 1; return 1;
} }
int sudokuJudge(struct Sudoku a){ int sudokuJudge(struct Sudoku a){
@ -100,56 +72,53 @@ int sudokuJudge(struct Sudoku a){
} }
void tagFindLeast(struct Sudoku *a, int *x, int *y, struct Sudoku *tags){ void tagFindLeast(struct Sudoku *a, int *x, int *y, struct Sudoku *tags){
int minv = 10; int minv = 10;
int mini=0, minj=0; *x = *y = 0;
range9(i) range81()
range9(j) if(!(a->inc[i][j])){
if(!a->v[i][j]){
int cnt = 0; int cnt = 0;
range9(k) range9(k)
cnt += (tags->v[i][j] >> k) & 1; cnt += !((tags->inc[i][j] >> (k+1)) & 1);
if(cnt < minv && cnt != 0){ if(cnt < minv && cnt != 0)
minv = cnt; minv = cnt,
mini = i; *x = i,
minj = j; *y = j;
}
} }
*x = mini;
*y = minj;
} }
struct Sudoku *sudokuFill__(struct Sudoku *a, int unfillednum, struct Sudoku *tags){ struct Sudoku *sudokuFill__(struct Sudoku *a, int unfillednum, struct Sudoku *tags){
struct Sudoku *pres, *t_tags; struct Sudoku *pres, *t_tags;
if(!unfillednum){ if(!unfillednum){
pres = ptrReg(); pres = M_Sudoku + tp ++;
sudokuCopy(*a, pres); sudokuCopy(*a, pres);
sudokuPrint(*tags); sudokuPrint(*a);
return pres; return pres;
} }
int m=0, n=0; int m=0, n=0;
tagFindLeast(a, &m, &n, tags); tagFindLeast(a, &m, &n, tags);
printf("%d\n", unfillednum);
range9(i) range9(i)
if(!((tags->v[m][n] >> i) & 1)){ if(!((tags->inc[m][n] >> (i+1)) & 1)){
a->v[m][n] = i; t_tags = M_Sudoku + tp ++;
t_tags = ptrReg();
sudokuCopy(*tags, t_tags); sudokuCopy(*tags, t_tags);
a->inc[m][n] = i+1;
tagPoint(a, m, n, t_tags); 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); pres = sudokuFill__(a, unfillednum - 1, t_tags);
if(pres) if(pres)
return pres; return pres;
a->v[m][n] = 0; a->inc[m][n] = 0;
ptrFree(); tp --;
} }
return NULL; return NULL;
} }
struct Sudoku *sudokuFill(struct Sudoku *a){ struct Sudoku *sudokuFill(struct Sudoku *a){
struct Sudoku tags; struct Sudoku tags, res, *pres;
sudokuClear(&tags); sudokuClear(&tags);
if(tagsInit(a, &tags)) return NULL; if(tagsInit(a, &tags)) return NULL;
int unfillednum = 0; int unfillednum = 0;
range9(i) range81()
range9(j) if(!a->inc[i][j])
if(!a->v[i][j])
unfillednum ++; unfillednum ++;
return sudokuFill__(a, unfillednum, &tags); return sudokuFill__(a, unfillednum, &tags);
return NULL;
} }
Loading…
Cancel
Save