int refresh_path(PATH *path, struct timeval *old, struct timeval *now, uint16_t speed) { assert(path); assert(old); assert(now); struct timeval diff; if (*(uint32_t *)&path->end == *(uint32_t *)&path->begin) return (0); evutil_timersub(old, now, &diff); if (diff.tv_sec <= 0) return (0); int path_distance = getdistance(&path->begin, &path->end); int move_distance = speed * diff.tv_sec + speed * diff.tv_usec / 1000000; if (move_distance >= path_distance) { *(uint32_t *)&path->begin = *(uint32_t *)&path->end; return (0); } float rate = move_distance / path_distance; int x = path->end.pos_x - path->begin.pos_x; int y = path->end.pos_y - path->begin.pos_y; path->begin.pos_x += x * rate; path->begin.pos_y += y * rate; return (0); }
int numberOfBoomerangs(vector<pair<int, int>>& points) { int n = points.size(); int ans = 0; for (int i = 0; i < n; i++) { vector<double> v; for (int j = 0; j < n; j++) { if (j == i) v.push_back(-1); else v.push_back(getdistance(points[i], points[j])); } sort(v.begin(), v.end()); double k = -1; int count = 0; for (int j = 0; j < n; j++) { if (k == v[j]) count++; else { ans += (count*(count - 1)); count = 1; k = v[j]; } } if(count!=1) ans += (count*(count - 1)); } return ans; }
int Breathing::isBreathing(IplImage* img, Facefeatures* face) //returns a binary value 0=bellow or 1=above for valid outputs, -1 if undefined { int b; int a = getdistance(img,face); // the position returned by the function above if (this->pos<200) // lower then 200 means that the list hasnt become full yet { this->list[this->pos]=a; this->pos++; return -1; //add the value to the list and return -1 as in not long enough list for comparison } else //list is full { if(this->pos==400) //the end of the list { this->pos-=200; //go back to the start } b=this->list[this->pos-200]; //pick the value that is on the spot that the new value will be added on this->list[this->pos-200]=a; // place the new value on that spot this->pos++; //increase the position variable if (this->avg==-1) //if a avg hasnt been calculated yet then loop threw the list and get the avg value { int i; for(i=0;i<200;i++) this->avg+=this->list[i]; } else //if avg has been set, then avg subtract the old value in that spot and adds the new value { avg=avg-b+a; } int val=0; for(int i=0;i<5;i++) { if(this->pos-i<200) { if (this->list[this->pos-i]*200<this->avg) //if the avg value is bigger then current value then add 1 to val; { val+=1; } } else { if (this->list[this->pos-i-200]*200<this->avg) //if the avg value is bigger then current value then add 1 to val; { val+=1; } } } if(val>2) { return 1; } else { return 0; } } }
char Agent::percept() { int x = head().X; int y = head().Y; //cout << "head : " << x << " , " << y << endl; string moves; if(Team()==1) moves="drul"; //harkat da jahate padsaat gard else moves="uldr"; // harkat da jahate saat gard for (int i =0;i<map->MapSize.X;i++) for (int j =0;j<map->MapSize.Y;j++) mark[i][j]=false; //hameye mark haro false mikone! hamaro pak mikone for (int i =0;i<map->MapSize.X;i++) for (int j =0;j<map->MapSize.Y;j++) enemy_mark[i][j]=false; //hameye enemy_mark haro false mikone! hamaro pak mikone for (int i =0;i<4;i++) for (int j =0;j<4;j++) score[i][j]=0; //hameye score haro 0 mikone for (int move=0;move<4;move++) // emtiaz dehi be har harekat { Point here_now; here_now.X=nextX(x,moves[move]); here_now.Y=nextY(y,moves[move]); //mark[here_now.X][here_now.Y]=true;//jaii ke mire! if (isEmpty(here_now.X,here_now.Y)) // age por bood ke hichi! { //cout << "x and y now :" << here_now.X << " , "<<here_now.Y; score[move][1]=painting(here_now,'m')+1; //chand ta khoone mitoone bere //cout << "painting of " << moves[move] <<" is : "<< score[move][1] << endl; Point enemy_head=ehead(); //sare yaroo ro mirize to enemy_head int enemy_score[4]={0}; // emtiaz haro hame 0 mokine! enemy_mark[here_now.X][here_now.Y]=true; //injaii ro ke alan hast mark mikone! for (int enemy_move=0;enemy_move<4;enemy_move++) //baraye har harekat harif mikhad emtiaz hesab bokone { Point enemy_pos; enemy_pos.X=nextX(enemy_head.X,moves[enemy_move]); enemy_pos.Y=nextY(enemy_head.Y,moves[enemy_move]); enemy_mark[enemy_pos.X][enemy_pos.Y]=true; if (isEmpty(enemy_pos.X,enemy_pos.Y)) { enemy_score[enemy_move]=painting(enemy_pos,'e'); //cout << "\t painting enemy " << moves[enemy_move] << " is : "<< enemy_score[enemy_move] << endl; //cout << "\t"<< "enemy pos: "<< enemy_pos.X << " , " << enemy_pos.Y << endl; for (int i =0;i<map->MapSize.Y;i++) //enemy mark ro 0 mikone for (int j =0;j<map->MapSize.X;j++) enemy_mark[i][j]=false; //hameye enemy_mark haro false mikone! hamaro pak mikone } } int minus=findmin(enemy_score,4,0); for (int i=0;i<4;i++) { if (enemy_score[i]!=0) enemy_score[i]=enemy_score[i]-minus; score[move][2]+=enemy_score[i]; //cout << "\t now enemy score of " << moves[i] <<" is " << enemy_score[i] <<endl; } score[move][3]=getdistance(here_now,ehead()); if (score[move][3] <= 1) {/*cout << "nazdiiiiiiiiiiik!"<<endl;*/ stuck(); } for (int i =0;i<map->MapSize.Y;i++) for (int j =0;j<map->MapSize.X;j++) mark[i][j]=false; //hameye mark haro false mikone! } } int min=10000; for (int i=0;i<4;i++) if (score[i][1]<min && score[i][1]!=0) min = score[i][1]; for (int i=0;i<4;i++) if (score[i][1]!=0) { score[i][1]=score[i][1]-min; //cout << "now the painting of " <<moves[i] << " is : " << score[i][1] <<"and the min is " << min <<endl; } min=10000; int min_id; for (int i=0;i<4;i++) if (score[i][3]<min && score[i][3]!=0) { min = score[i][3]; min_id=i; } for (int i=0;i<4;i++) { if (i!=min_id) score[i][3]=0; else score[i][3]=2; /////////////////////////////////////////////////////////////////////////////////////////////////// } //cout << "now the closest is : "<< moves[min_id] << " Point : " << score[min_id][3] << endl; //jamm score ha for (int move=0;move<4;move++) { score[move][0]+=score[move][1]+score[move][3]-score[move][2]; } int max_char_id=0; int max_score=0; for (int move=0;move<4;move++) { if (max_score<score[move][0]) { //cout <<"score move "<< moves[move] <<" is bigger than previouse :"<< score[move][0] <<endl; max_score = score[move][0]; max_char_id = move; } } cout << "our algorithm output: "<< moves[max_char_id] <<endl << "\t and the score is " << max_score<<endl ; if (max_score!=0) return moves[max_char_id]; return stuck(); //return stuck(); }