void Trial(int i,int n) { int k,m,j; if(i==n) { printf("八皇后\n"); for(k=0;k<n;k++) { for(m=0;m<n;m++) { printf("%4d",a[k][m]); } printf("\n"); } w++; //exit(0); } else { for(j=0;j<n;j++) { a[i][j]=1; if(Valid(i,j)) { //printf("i=%d,j=%d\n",i,j); //printf("\n"); Trial(i+1,n); } a[i][j]=0; } } }
int main() { int i,j; for(i=0; i<SCALE; i++) { for(j=0; j<SCALE; j++) chessboard[i][j]='#'; } Trial(0); printf("Total number of valid layout is %d.\n\n",count); return 0; }
VNode* DESPOT::ConstructTree(vector<State*>& particles, RandomStreams& streams, ScenarioLowerBound* lower_bound, ScenarioUpperBound* upper_bound, const DSPOMDP* model, History& history, double timeout, SearchStatistics* statistics) { if (statistics != NULL) { statistics->num_particles_before_search = model->NumActiveParticles(); } for (int i = 0; i < particles.size(); i++) { particles[i]->scenario_id = i; } VNode* root = new VNode(particles); logd << "[DESPOT::ConstructTree] START - Initializing lower and upper bounds at the root node."; InitBounds(root, lower_bound, upper_bound, streams, history); logd << "[DESPOT::ConstructTree] END - Initializing lower and upper bounds at the root node."; if (statistics != NULL) { statistics->initial_lb = root->lower_bound(); statistics->initial_ub = root->upper_bound(); } double used_time = 0; int num_trials = 0; do { double start = clock(); VNode* cur = Trial(root, streams, lower_bound, upper_bound, model, history, statistics); used_time += double(clock() - start) / CLOCKS_PER_SEC; start = clock(); Backup(cur); if (statistics != NULL) { statistics->time_backup += double(clock() - start) / CLOCKS_PER_SEC; } used_time += double(clock() - start) / CLOCKS_PER_SEC; num_trials++; } while (used_time * (num_trials + 1.0) / num_trials < timeout && (root->upper_bound() - root->lower_bound()) > 1e-6); if (statistics != NULL) { statistics->num_particles_after_search = model->NumActiveParticles(); statistics->num_policy_nodes = root->PolicyTreeSize(); statistics->num_tree_nodes = root->Size(); statistics->final_lb = root->lower_bound(); statistics->final_ub = root->upper_bound(); statistics->time_search = used_time; statistics->num_trials = num_trials; } return root; }
int main(void) { int a[MAX_LEN][MAX_LEN]; for (int i = 0; i < MAX_LEN; i++) { for (int j = 0; j < MAX_LEN; j++) { a[i][j] = 0; } } Trial(a, 0); printf("%d", times); }
Trial(int a[MAX_LEN][MAX_LEN], int i) { if (i == MAX_LEN) print(a); int j = 0; for (; j < MAX_LEN ; j++) { if (!IsSameLine(a, i, j)) { //作为标记,1表示摆放 a[i][j] = 1; i++; Trial(a, i); //不满足要求之后,要恢复原值 i--; a[i][j] = 0; } } }
void Trial(int i) { // 进入本函数时,在SCALE×SCALE棋盘前的i-1行已放置了互不攻击的i-1个棋子 // 现从第i行起继续为后续棋子选择合适的位置 // 当i>n时,秋的一个合法布局,输出之 int j; if(i>=SCALE) { printf("No. %d\n",count); PrintBoard(); count++; } else for(j=0; j<SCALE; j++) { PlaceChess(i,j); if(TRUE==IsValidLayout()) Trial(i+1); RemoveChess(i,j); } }
double DESPOT::CheckDESPOTSTAR(const VNode* vnode, double regularized_value) { cout << "--------------------------------------------------------------------------------" << endl; const vector<State*>& particles = vnode->particles(); vector<State*> copy; for (int i = 0; i < particles.size(); i++) { copy.push_back(model_->Copy(particles[i])); } VNode* root = new VNode(copy); RandomStreams streams = RandomStreams(Globals::config.num_scenarios, Globals::config.search_depth); InitBounds(root, lower_bound_, upper_bound_, streams, history_); double used_time = 0; int num_trials = 0; do { double start = clock(); VNode* cur = Trial(root, streams, lower_bound_, upper_bound_, model_, history_); num_trials++; used_time += double(clock() - start) / CLOCKS_PER_SEC; start = clock(); Backup(cur); used_time += double(clock() - start) / CLOCKS_PER_SEC; } while (root->lower_bound() < regularized_value); cout << "DESPOT: # trials = " << num_trials << "; target = " << regularized_value << ", current = " << root->lower_bound() << ", l = " << root->lower_bound() << ", u = " << root->upper_bound() << "; time = " << used_time << endl; cout << "--------------------------------------------------------------------------------" << endl; root->Free(*model_); delete root; return used_time; }
double DESPOT::CheckDESPOT(const VNode* vnode, double regularized_value) { cout << "--------------------------------------------------------------------------------" << endl; const vector<State*>& particles = vnode->particles(); vector<State*> copy; for (int i = 0; i < particles.size(); i ++) { copy.push_back(model_->Copy(particles[i])); } VNode* root = new VNode(copy); double pruning_constant = Globals::config.pruning_constant; Globals::config.pruning_constant = 0; RandomStreams streams = RandomStreams(Globals::config.num_scenarios, Globals::config.search_depth); streams.position(0); InitBounds(root, lower_bound_, upper_bound_, streams, history_); double used_time = 0; int num_trials = 0, prev_num = 0; double pruned_value; do { double start = clock(); VNode* cur = Trial(root, streams, lower_bound_, upper_bound_, model_, history_); num_trials++; used_time += double(clock() - start) / CLOCKS_PER_SEC; start = clock(); Backup(cur); used_time += double(clock() - start) / CLOCKS_PER_SEC; if (double(num_trials - prev_num) > 0.05 * prev_num) { int pruned_action; Globals::config.pruning_constant = pruning_constant; VNode* pruned = Prune(root, pruned_action, pruned_value); Globals::config.pruning_constant = 0; prev_num = num_trials; pruned->Free(*model_); delete pruned; cout << "# trials = " << num_trials << "; target = " << regularized_value << ", current = " << pruned_value << ", l = " << root->lower_bound() << ", u = " << root->upper_bound() << "; time = " << used_time << endl; if (pruned_value >= regularized_value) { break; } } } while (true); cout << "DESPOT: # trials = " << num_trials << "; target = " << regularized_value << ", current = " << pruned_value << ", l = " << root->lower_bound() << ", u = " << root->upper_bound() << "; time = " << used_time << endl; Globals::config.pruning_constant = pruning_constant; cout << "--------------------------------------------------------------------------------" << endl; root->Free(*model_); delete root; return used_time; }
int main() { int i,j; Trial(0,N); printf("一共有%d种",w); }