0

我在板上有路径,例如索引对列表,例如

list<pair<int,int> > *path;

当角色到达障碍物时,我需要用新路径替换所有从当前位置到结束的路径,但要保存从起点到障碍物的路径相同(以澄清一个小角色具有起点和目标单元格并从起点移动到终点并返回并再次重复相同。为了避免大量计算,我会尝试保存没有障碍的部分路径,并将旧部分替换为从障碍到结束的新部分)。有没有简单的方法可以在不迭代的情况下做到这一点?

稍后编辑以澄清:旧路径是 [(0,0),(0,1),(1,1),(1,2),(1,3),(1,4)] 并且障碍物在单元格上(1,2) 所以字符去 [(0,0),(0,1),(1,1)] 搜索新路径,例如 [(1,1),(2,1),(2 ,2),(1,3),(1,4)] 所以我需要从旧路径 (1,1),(1,2),(1,3),(1,4) 替换为 (1 ,1),(2,1),(2,2),(1,3),(1,4)

4

1 回答 1

1

If I understand your question correctly: You have a list path from start to destination, which hits an obstacle somewhere in the middle (you have to know where, so let's say you have a valid list<...>::iterator obstacle that points to the obstacle). You also have a newpath going from *(obstacle - 1) to destination, and you want to cut and paste those two paths together. This is how you could do it (I'm assuming lists instead of list*s here, but if you really need that you can easily rewrite it to work with the pointer):

//kill the wrong bit of the old path
path.erase(obstacle, path.end());
//append the correct newpath
path.splice(path.end(), newpath, newpath.begin(), newpath.end());

Note that newpath is empty after this operation.

于 2013-02-24T03:39:15.930 回答