J'ai une fonction de remplissage d'inondation dans mon code qui renvoie juste un nombre sur les cellules remplies. Mais il est atrocement lent, mon algorithme de recherche de chemin A * est exponentiellement plus rapide que lui. Voici l'extrait:Comment rendre mon algorithme de remplissage d'inondation plus efficace?
bool withinBoundaries(_2D::Point p) {
//cerr << "Checking if x: " << p.x << " y: " << p.y << " is within boundaries" << endl;
if (p.x <= 29 && p.y <= 19 && p.x >= 0 && p.y >= 0) {
return true;
}
return false;
}
bool canMove(_2D::Point point, _2D::Point offset, map<pair<int, int>, bool> gameGrid) {
if (!gameGrid[(point + offset).toPair()] && withinBoundaries(point + offset)) return true;
else return false;
}
int floodFillGetArea(_2D::Point point, map<pair<int, int>, bool> gameGrid) {
map<pair<int, int>, bool> checked;
map<pair<int, int>, bool> gameGridCopy = gameGrid;
deque<_2D::Point> openPoints;
openPoints.push_back(point);
int count = 0;
while (!openPoints.empty()) {
_2D::Point curPoint = openPoints.back();
openPoints.pop_back();
if(checked[curPoint.toPair()]) break;
gameGridCopy[curPoint.toPair()] = true;
count++;
if (canMove(curPoint, _2D::Point::UP(), gameGridCopy)) {
openPoints.push_back(curPoint + _2D::Point::UP());
}
if (canMove(curPoint, _2D::Point::RIGHT(), gameGridCopy)) {
openPoints.push_back(curPoint + _2D::Point::RIGHT());
}
if (canMove(curPoint, _2D::Point::DOWN(), gameGridCopy)) {
openPoints.push_back(curPoint + _2D::Point::DOWN());
}
if (canMove(curPoint, _2D::Point::LEFT(), gameGridCopy)) {
openPoints.push_back(curPoint + _2D::Point::LEFT());
}
checked[curPoint.toPair()] = true;
}
return count;
}
Ceci vérifie un nœud toutes les secondes, pourquoi est-il si lent? Et _2D :: Point est juste un objet avec int x et int y. Aussi, il crée des hochements de tête ouverts quand ils sont déjà étiquetés comme étant vérifiés, pourquoi?
Vous passez 'gameGrid' et' checked' par la copie au lieu de const const ... – Jarod42
Je pense que vous aurez une meilleure réponse sur http://codereview.stackexchange.com/ – Garf365
gameGrid est statique c'est juste un test la fonction le manipule pour vérifier la zone. Aussi vérifié est censé être local Je ne sais pas pourquoi je mets cela comme une fonction – bi0phaz3