코드에 채워진 셀의 숫자를 반환하는 플러드 채우기 기능이 있습니다. 하지만 너무 느리게, 내 A * 길 찾기 알고리즘이 지수 적으로 빠릅니다. 스 니펫은 다음과 같습니다.홍수 채우기 알고리즘을보다 효율적으로 만들려면 어떻게해야합니까?
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;
}
이것은 1 초마다 노드를 검사합니다. 왜 그렇게 느립니다? 그리고 _2D :: Point는 int x와 int y를 가진 객체 일뿐입니다. 또한 이미 확인 된 것으로 레이블이 지정되어있는 경우 열린 노드를 만듭니다. 그 이유는 무엇입니까?
당신은 단지 시험의 정적 당신이 http://codereview.stackexchange.com/ – Jarod42
을'gameGrid'을 통과하고 이 함수는 영역을 확인하기 위해 조작합니다. 또한 체크 된 것은 로컬 일 것입니다. 저는 왜 이것을 함수의 매개 변수로 넣을 지 모르겠습니다. – Garf365
gameGrid에 더 나은 해답을 갖고있을 것 ... 사본 대신 const를 참조하여 checked'' – bi0phaz3