void Backtracking(int i) { if(i>n) { Print(); if(bestPrice>bp) { bp=bestPrice; for(int j=1;j<=n;j++) bA[j]=bestAnswer[j]; } return; } if(currentWeight+weight[i]<=c) { //将物品i放入背包,搜索左子树 bestAnswer[i] = 1; currentWeight += weight[i]; bestPrice += price[i]; Backtracking(i+1); //完成上面的递归,返回到上一结点,物品i不放入背包,准备递归右子树 currentWeight -= weight[i]; bestPrice -= price[i]; } bestAnswer[i] = 0; Backtracking(i+1); }
void Backtracking(int now) { if(now >= 12) return; if(now > 0) { int i, f = 1, c = now; int tmp = 2, count = 2; for(i = 2; i <= c+1; i++) f *= i; for(i = 0; i < c-1; i++) { if(step[i] == step[i+1] && step[i] == 2) count++, tmp *= count; else { if(step[i] == 2) f /= tmp, tmp = 2, count = 2; } } if(step[i] == 2) f /= tmp, tmp = 2, count = 2; ans[c+1] += f; } step[now] = 0; Backtracking(now+1); step[now] = 2; Backtracking(now+1); }
void Backtracking(int l, int r) { if(l+1 < r) { int m = Wy[l][r]; printf("("); Backtracking(l, m); printf(" x "); Backtracking(m+1, r); printf(")"); } if(l == r) printf("A%d", l+1); if(l+1 == r) printf("(A%d x A%d)", l+1, r+1); }
vector<string> readBinaryWatch(int num) { if (num == 0) { result.push_back("0:00"); return result; } vector<bool> leds(10, false); Backtracking(leds, 0, num); return result; }
int main() { int t, n; Backtracking(0); scanf("%d", &t); while(t--) { scanf("%d", &n); printf("%d\n", ans[n]); } return 0; }
void Backtracking(vector<bool>& leds, int index, int num) { if (index >= 10 || num <= 0) { if (num == 0) { int hour = 0, minute = 0; for (int i = 0; i < 4; ++i) { if (leds.at(i)) { int shift = 4 - i - 1; hour += (1 << shift); } } for (int i = 4; i < 10; ++i) { if (leds.at(i)) { int shift = 6 - (i - 4) - 1; minute += (1 << shift); } } if (hour >= 12 || minute >= 60) { // 0:00 to 11:59 return; } string tmp; tmp.append(to_string(hour)); tmp.append(":"); if (minute < 10) { tmp.append("0"); } tmp.append(to_string(minute)); result.push_back(tmp); } return; } //off Backtracking(leds, index + 1, num); //on leds.at(index) = true; Backtracking(leds, index + 1, num - 1); leds.at(index) = false; }
void main() { int i; printf("请输入物品的数量:\n"); scanf("%d",&n); printf("请输入背包的容量(能承受的重量):\n"); scanf("%d",&c); printf("请依次输入%d个物品的重量:\n",n); for(i=1;i<=n;i++) scanf("%d",&weight[i]); printf("请依次输入%d个物品的价值:\n",n); for(i=1;i<=n;i++) scanf("%d",&price[i]); printf("各符合条件的路径为:\n"); Backtracking(1); printf("*******************************************************\n"); printf("\nthe best answer is {"); for(i=1;i<n;++i) printf("%d,",bA[i]); printf("%d}\tthe price is %d\n",bA[i],bp); }
main() { int n, A[11], C = 0; while(scanf("%d", &n) == 1 && n) { for(a = 0; a < n; a++) scanf("%d %d", &A[a], &A[a+1]); for(a = 1; a < n; a++) { for(b= 0, c= a + b; c < n; b++, c++) { int min = 2147483647, t, set; for(d = b; d < c; d++) { t = DP[b][d] + DP[d+1][c] + A[b]*A[d+1]*A[c+1]; if(min > t) { min = t, set = d; } } DP[b][c] = min, Wy[b][c] = set; } } printf("Case %d: ", ++C); Backtracking(0, n-1); puts(""); } return 0; }
/*! \fn solve system with Newton-Raphson * * \param [in] [n] size of equation * [eps] tolerance for x * [h] tolerance for f' * [k] maximum number of iterations * [work] work array size of (n*X) * [f] user provided function * [data] userdata * [info] * [calculate_jacobian] flag which decides whether Jacobian is calculated * (0) once for the first calculation * (i) every i steps (=1 means original newton method) * (-1) never, factorization has to be given in A * */ int _omc_newton(int(*f)(int*, double*, double*, void*, int), DATA_NEWTON* solverData, void* userdata) { int i, j, k = 0, l = 0, nrsh = 1; int *n = &(solverData->n); double *x = solverData->x; double *fvec = solverData->fvec; double *eps = &(solverData->ftol); double *fdeps = &(solverData->epsfcn); int * maxfev = &(solverData->maxfev); double *fjac = solverData->fjac; double *work = solverData->rwork; int *iwork = solverData->iwork; int *info = &(solverData->info); int calc_jac = 1; double error_f = 1.0 + *eps, scaledError_f = 1.0 + *eps, delta_x = 1.0 + *eps, delta_f = 1.0 + *eps, delta_x_scaled = 1.0 + *eps, lambda = 1.0; double current_fvec_enorm, enorm_new; if(ACTIVE_STREAM(LOG_NLS_V)) { infoStreamPrint(LOG_NLS_V, 1, "######### Start Newton maxfev: %d #########", (int)*maxfev); infoStreamPrint(LOG_NLS_V, 1, "x vector"); for(i=0; i<*n; i++) infoStreamPrint(LOG_NLS_V, 0, "x[%d]: %e ", i, x[i]); messageClose(LOG_NLS_V); messageClose(LOG_NLS_V); } *info = 1; /* calculate the function values */ (*f)(n, x, fvec, userdata, 1); solverData->nfev++; /* save current fvec in f_old*/ memcpy(solverData->f_old, fvec, *n*sizeof(double)); error_f = current_fvec_enorm = enorm_(n, fvec); while(error_f > *eps && scaledError_f > *eps && delta_x > *eps && delta_f > *eps && delta_x_scaled > *eps) { if(ACTIVE_STREAM(LOG_NLS_V)) { infoStreamPrint(LOG_NLS_V, 0, "\n**** start Iteration: %d *****", (int) l); /* Debug output */ infoStreamPrint(LOG_NLS_V, 1, "function values"); for(i=0; i<*n; i++) infoStreamPrint(LOG_NLS_V, 0, "fvec[%d]: %e ", i, fvec[i]); messageClose(LOG_NLS_V); } /* calculate jacobian if no matrix is given */ if (calc_jac == 1 && solverData->calculate_jacobian >= 0) { (*f)(n, x, fvec, userdata, 0); solverData->factorization = 0; calc_jac = solverData->calculate_jacobian; } else { solverData->factorization = 1; calc_jac--; } /* debug output */ if(ACTIVE_STREAM(LOG_NLS_JAC)) { char buffer[4096]; infoStreamPrint(LOG_NLS_JAC, 1, "jacobian matrix [%dx%d]", (int)*n, (int)*n); for(i=0; i<solverData->n;i++) { buffer[0] = 0; for(j=0; j<solverData->n; j++) sprintf(buffer, "%s%10g ", buffer, fjac[i*(*n)+j]); infoStreamPrint(LOG_NLS_JAC, 0, "%s", buffer); } messageClose(LOG_NLS_JAC); } if (solveLinearSystem(n, iwork, fvec, fjac, solverData) != 0) { *info=-1; break; } else { for (i =0; i<*n; i++) solverData->x_new[i]=x[i]-solverData->x_increment[i]; infoStreamPrint(LOG_NLS_V,1,"x_increment"); for(i=0; i<*n; i++) infoStreamPrint(LOG_NLS_V, 0, "x_increment[%d] = %e ", i, solverData->x_increment[i]); messageClose(LOG_NLS_V); if (solverData->newtonStrategy == NEWTON_DAMPED) { damping_heuristic(x, f, current_fvec_enorm, n, fvec, &lambda, &k, solverData, userdata); } else if (solverData->newtonStrategy == NEWTON_DAMPED2) { damping_heuristic2(0.75, x, f, current_fvec_enorm, n, fvec, &k, solverData, userdata); } else if (solverData->newtonStrategy == NEWTON_DAMPED_LS) { LineSearch(x, f, current_fvec_enorm, n, fvec, &k, solverData, userdata); } else if (solverData->newtonStrategy == NEWTON_DAMPED_BT) { Backtracking(x, f, current_fvec_enorm, n, fvec, solverData, userdata); } else { /* calculate the function values */ (*f)(n, solverData->x_new, fvec, userdata, 1); solverData->nfev++; } calculatingErrors(solverData, &delta_x, &delta_x_scaled, &delta_f, &error_f, &scaledError_f, n, x, fvec); /* updating x */ memcpy(x, solverData->x_new, *n*sizeof(double)); /* updating f_old */ memcpy(solverData->f_old, fvec, *n*sizeof(double)); current_fvec_enorm = error_f; /* check if maximum iteration is reached */ if (++l > *maxfev) { *info = -1; warningStreamPrint(LOG_NLS_V, 0, "Warning: maximal number of iteration reached but no root found"); break; } } if(ACTIVE_STREAM(LOG_NLS_V)) { infoStreamPrint(LOG_NLS_V,1,"x vector"); for(i=0; i<*n; i++) infoStreamPrint(LOG_NLS_V, 0, "x[%d] = %e ", i, x[i]); messageClose(LOG_NLS_V); printErrors(delta_x, delta_x_scaled, delta_f, error_f, scaledError_f, eps); } } solverData->numberOfIterations += l; solverData->numberOfFunctionEvaluations += solverData->nfev; return 0; }