So I'm working on implementing the A* algorithm. I have a class Node described below. It has x and y coordinates and G and H properties.
#include <iostream>
#include <algorithm>
#include <vector>
class Node
{
public:
explicit Node(){};
explicit Node(int a, int b, int c, int d){
x=a;
y=b;
G=c;
H=d;
}
Node operator=(Node other){
this->x = other.x;
this->y = other.y;
this->G = other.G;
this->H = other.H;
return *this;
}
int x{0};
int y{0};
int G{0};
int H{0};
};
I'm trying to use the sort() algorithm to sort the array of Nodes by the value of the H property
bool Compare(const Node a, const Node b)
{
int f1 = a.G + a.H; // f1 = g1 + h1
int f2 = b.G + b.H; // f2 = g2 + h2
return f1>f2;
}
void CellSort(vector<Node>* v){
sort(v->begin(), v->end(), Compare);
}
This is the main function. I can't figure out why it's not working. There is no error printed. I've studied how the sort() algorithm works and looked at similar questions posted. Still can't figure it out.
int main( ) {
vector<Node> open;
Node n[5];
n[0] = Node(0,1,1,2);
n[1] = Node(1,0,1,1);
n[2] = Node(1,1,1,5);
n[3] = Node(2,1,1,3);
n[4] = Node(1,2,1,4);
for(int i=0;i<5;i++)
open.push_back(n[i]);
CellSort(&open);
for(int i=0;i<5;i++)
cout<<n[i].x<<" "<<n[i].y<<" "<<n[i].G<<" "<<n[i].H<<"\n";
return 0;
}
Aucun commentaire:
Enregistrer un commentaire