path aStar(map c, coord p, finish f){ int end=0; int i,j; list* open=createList(); list* closed=createList(); node* startNode=createNode(p); node* current; node* neighbor; addElement(open,startNode); coord bestFinish=getbestFinish(p,f); while(!end) { current= removeElement(open); addElement(closed,current); end=isFinish(current->coord,f); for(i=-1;i<=1;i++) { for(j=-1;j<=1;j++) { if(i!=0 || j!=0) { neighbor=createNode(createCoord(current->coord.x+i, current->coord.y+j)); if(! (isObstacle(neighbor->coord,c) || contains(closed,neighbor))) { if(contains(open,neighbor)) { if(current->cost+1 < neighbor->cost) { neighbor->cost= current->cost+1; if(isSand(neighbor->coord,c)) { neighbor->cost += 4; } neighbor->pred=current; } } else { neighbor->pred=current; neighbor->cost=current->cost+1; if(isSand(neighbor->coord,c)) { neighbor->cost += 4; } neighbor->heuristic=neighbor->cost=current->cost + neighbor->cost+norm(substractCoords(bestFinish,current->coord)); addSortedElement(open,neighbor); } } } } } } freeList(closed); freeList(open); path path=reconstructPath(current); return path; }
SortedList *listSum(SortedList *firstList, SortedList *secondList) { SortedList *list = createSortedList(); SortedListElement *pointer = firstList->head->next; while (pointer != nullptr) { addSortedElement(list, pointer->power, pointer->coeff); pointer = pointer->next; } pointer = secondList->head->next; while (pointer != nullptr) { addSortedElement(list, pointer->power, pointer->coeff); pointer = pointer->next; } return list; }