int get(int key) { if(dic.find(key) == dic.end()) return -1; else { set_front(key); return dic[key]; } }
void set(int key, int value) { if(dic.find(key) != dic.end()) { dic[key] = value; set_front(key); } else { dic[key] = value; lru.push_front(key); erase_last(); } }
void astar_algo(node* start,node* goal){ //open_list.push_back(start) start->state= 1; openset.insert(start); list<node*> n_list; int count=0; int step_count =0; while(!openset.empty()){ node* q; //q = find_minf(open_list); q = set_front(); print_vec(q->id); if(q==goal){ cout << "found the goal" <<endl; //return; cout<<endl<<"s"<<endl; print_vec(q->id); cout<<endl; count++; while(q->parent!=NULL){ print_vec((q->parent)->id); q = q->parent; cout<<endl; count++; } cout<<"count: "<<count<<endl; cout<<"step count: "<<step_count<<endl; return; } else { step_count++; //close_list.push_back(q); //open_list.remove(q); remove_setfront(); n_list = get_neighbour_puzzle(q); list<node*>:: const_iterator iterator; for (iterator = n_list.begin(); iterator != n_list.end(); ++iterator) { if((*iterator)->state == 0) { (*iterator)->parent = q; (*iterator)->g_val = q->g_val + adj_dist(); (*iterator)->h_val = heuristic_puzzle(*iterator); (*iterator)->state =1; openset.insert((*iterator)); } else if((*iterator)->state == 1) { int tmp = q->g_val + adj_dist(); if((*iterator)->g_val > tmp) { node * n = (*iterator); openset.erase(n); n->g_val = tmp; n->parent = q; openset.insert(n); } } else { int tmp = q->g_val + adj_dist(); if((*iterator)->g_val > tmp) { node * n = (*iterator); n->g_val = tmp; n->state =1; n->parent = q; openset.insert(n); } } } } } cout << "Empty openlist.Should not come here" <<endl; }