void Sudoku::SolveIt() { if(pointer==block_num) { sol++; if(sol==1) { for(int i=0; i<SIZE; i++) for(int j=0; j<SIZE; j++) ans[i][j]=map[i][j]; } return ; } for(int i=0; i<9; i++) { if(block[pointer].num[i]!=0) { map[block[pointer].i][block[pointer].j]= i+1; // printf("--------------------------------------------\n"); //print(map); if(check_repeat_num(block[pointer].i,block[pointer].j)==1) { pointer++;//合格進入下一層 SolveIt(); pointer--;//恢復 map[block[pointer].i][block[pointer].j]= 0 ;//恢復一切 } else map[block[pointer].i][block[pointer].j]= 0 ;//檢查不合格 恢復一切 } } /*for(int i=8; i>=0; i--) { if(block[pointer].num[i]!=0) { map[block[pointer].i][block[pointer].j]= i+1; //printf("--------------------------------------------\n"); //print(map); if(check_repeat_num(block[pointer].i,block[pointer].j)==1) { pointer++;//合格進入下一層 SolveIt(); pointer--;//恢復 map[block[pointer].i][block[pointer].j]= 0 ;//恢復一切 } else map[block[pointer].i][block[pointer].j]= 0 ;//檢查不合格 恢復一切 } }*/ }
void Sudoku::Solve() { SolveIt(); if(sol==1) { printf("1\n"); print(ans); } if(sol==0) printf("2"); }
int main() { UserData data; realtype fnormtol, scsteptol; N_Vector u1, u2, u, s, c; int glstr, mset, flag; void *kmem; u1 = u2 = u = NULL; s = c = NULL; kmem = NULL; data = NULL; /* User data */ data = (UserData)malloc(sizeof *data); data->lb[0] = PT25; data->ub[0] = ONE; data->lb[1] = ONEPT5; data->ub[1] = TWO*PI; /* Create serial vectors of length NEQ */ u1 = N_VNew_Serial(NEQ); if (check_flag((void *)u1, "N_VNew_Serial", 0)) return(1); u2 = N_VNew_Serial(NEQ); if (check_flag((void *)u2, "N_VNew_Serial", 0)) return(1); u = N_VNew_Serial(NEQ); if (check_flag((void *)u, "N_VNew_Serial", 0)) return(1); s = N_VNew_Serial(NEQ); if (check_flag((void *)s, "N_VNew_Serial", 0)) return(1); c = N_VNew_Serial(NEQ); if (check_flag((void *)c, "N_VNew_Serial", 0)) return(1); SetInitialGuess1(u1,data); SetInitialGuess2(u2,data); N_VConst_Serial(ONE,s); /* no scaling */ Ith(c,1) = ZERO; /* no constraint on x1 */ Ith(c,2) = ZERO; /* no constraint on x2 */ Ith(c,3) = ONE; /* l1 = x1 - x1_min >= 0 */ Ith(c,4) = -ONE; /* L1 = x1 - x1_max <= 0 */ Ith(c,5) = ONE; /* l2 = x2 - x2_min >= 0 */ Ith(c,6) = -ONE; /* L2 = x2 - x22_min <= 0 */ fnormtol=FTOL; scsteptol=STOL; kmem = KINCreate(); if (check_flag((void *)kmem, "KINCreate", 0)) return(1); flag = KINSetUserData(kmem, data); if (check_flag(&flag, "KINSetUserData", 1)) return(1); flag = KINSetConstraints(kmem, c); if (check_flag(&flag, "KINSetConstraints", 1)) return(1); flag = KINSetFuncNormTol(kmem, fnormtol); if (check_flag(&flag, "KINSetFuncNormTol", 1)) return(1); flag = KINSetScaledStepTol(kmem, scsteptol); if (check_flag(&flag, "KINSetScaledStepTol", 1)) return(1); flag = KINInit(kmem, func, u); if (check_flag(&flag, "KINInit", 1)) return(1); /* Call KINDense to specify the linear solver */ flag = KINDense(kmem, NEQ); if (check_flag(&flag, "KINDense", 1)) return(1); /* Print out the problem size, solution parameters, initial guess. */ PrintHeader(fnormtol, scsteptol); /* --------------------------- */ printf("\n------------------------------------------\n"); printf("\nInitial guess on lower bounds\n"); printf(" [x1,x2] = "); PrintOutput(u1); N_VScale_Serial(ONE,u1,u); glstr = KIN_NONE; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u1,u); glstr = KIN_LINESEARCH; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u1,u); glstr = KIN_NONE; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u1,u); glstr = KIN_LINESEARCH; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ printf("\n------------------------------------------\n"); printf("\nInitial guess in middle of feasible region\n"); printf(" [x1,x2] = "); PrintOutput(u2); N_VScale_Serial(ONE,u2,u); glstr = KIN_NONE; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u2,u); glstr = KIN_LINESEARCH; mset = 1; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u2,u); glstr = KIN_NONE; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* --------------------------- */ N_VScale_Serial(ONE,u2,u); glstr = KIN_LINESEARCH; mset = 0; SolveIt(kmem, u, s, glstr, mset); /* Free memory */ N_VDestroy_Serial(u); N_VDestroy_Serial(s); N_VDestroy_Serial(c); KINFree(&kmem); free(data); return(0); }