多次运行洪水填充算法时遇到堆栈溢出异常
我有一个排列在网格上的对象列表。我想删除任何没有返回网格顶部的路径的东西,无论是直接的还是通过邻居的。
我认为执行此操作的一个好方法是首先使网格上的所有内容都可删除,然后使顶行上的所有内容不可删除。然后,我想对顶行中的任何对象进行洪水填充,以使连接到它们的对象不可删除。
我想不出一种(有效的)方法来优化它。有没有更简单的方法来完成我想做的事情?
使用列表而不是二维数组可能会搬起石头砸自己的脚。它引入了许多额外的 foreach 循环。
internal void DetectHangingObjects(int X)
{
//Set all objects to deletable
for (int i = 0; i < planets.Count; i++)
planets[i].deletable = true;
//Set first row to not deletable
for (int i = 0; i < planets.Count; i++)
{
Debug.WriteLine(planets[i].X + " " + X);
if (planets[i].X == X) //X=0
{
planets[i].deletable = false;
try
{
DetectHangingNeighbours(planets[i]);
}
catch (Exception ee)
{ Debug.WriteLine(ee.Message); }
}
}
}
internal void DetectHangingNeighbours(Planet planet)
{
if (planet == null || planet.deletable==true)
return;
planet.deletable = false;
DetectHangingNeighbours(GetTopLeftNode2(planet));
DetectHangingNeighbours(GetTopRightNode2(planet));
DetectHangingNeighbours(GetLeftNode2(planet));
DetectHangingNeighbours(GetRightNode2(planet));
DetectHangingNeighbours(GetBottomLeftNode2(planet));
DetectHangingNeighbours(GetBottomRightNode2(planet));
}
//The following methods check the six adjacent objects and returns them to the caller if they match
internal Planet GetTopLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X - planetSize && gridPlanet.Y == planet.Y - yOffset)
return gridPlanet;
return null;
}
internal Planet GetTopRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X - planetSize && gridPlanet.Y == planet.Y + yOffset)
return gridPlanet;
return null;
}
internal Planet GetLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X && gridPlanet.Y == planet.Y - planetSize)
return gridPlanet;
return null;
}
internal Planet GetRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X && gridPlanet.Y == planet.Y + planetSize)
return gridPlanet;
return null;
}
internal Planet GetBottomLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X + planetSize && gridPlanet.Y == planet.Y - yOffset)
return gridPlanet;
return null;
}
internal Planet GetBottomRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X + planetSize && gridPlanet.Y == planet.Y + yOffset)
return gridPlanet;
return null;
}
I have a list of objects arranged on a grid. I want to remove any which don't don't have a path back to the top of the grid, either directly or through their neighbours.
I thought a good way to do this would be to first make everything on the grid deletable, then make anything on the top row not deletable. Then, I wanted to do a flood fill from any objects in the top row, to make the objects connected to them not deletable.
I can't think of a way (that works) to optimize it. Is there a simpler way to go about what I'm trying to do?
I may have shot myself in the foot by using a list, rather than a 2d array. It's introducing a lot of extra foreach loops.
internal void DetectHangingObjects(int X)
{
//Set all objects to deletable
for (int i = 0; i < planets.Count; i++)
planets[i].deletable = true;
//Set first row to not deletable
for (int i = 0; i < planets.Count; i++)
{
Debug.WriteLine(planets[i].X + " " + X);
if (planets[i].X == X) //X=0
{
planets[i].deletable = false;
try
{
DetectHangingNeighbours(planets[i]);
}
catch (Exception ee)
{ Debug.WriteLine(ee.Message); }
}
}
}
internal void DetectHangingNeighbours(Planet planet)
{
if (planet == null || planet.deletable==true)
return;
planet.deletable = false;
DetectHangingNeighbours(GetTopLeftNode2(planet));
DetectHangingNeighbours(GetTopRightNode2(planet));
DetectHangingNeighbours(GetLeftNode2(planet));
DetectHangingNeighbours(GetRightNode2(planet));
DetectHangingNeighbours(GetBottomLeftNode2(planet));
DetectHangingNeighbours(GetBottomRightNode2(planet));
}
//The following methods check the six adjacent objects and returns them to the caller if they match
internal Planet GetTopLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X - planetSize && gridPlanet.Y == planet.Y - yOffset)
return gridPlanet;
return null;
}
internal Planet GetTopRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X - planetSize && gridPlanet.Y == planet.Y + yOffset)
return gridPlanet;
return null;
}
internal Planet GetLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X && gridPlanet.Y == planet.Y - planetSize)
return gridPlanet;
return null;
}
internal Planet GetRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X && gridPlanet.Y == planet.Y + planetSize)
return gridPlanet;
return null;
}
internal Planet GetBottomLeftNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X + planetSize && gridPlanet.Y == planet.Y - yOffset)
return gridPlanet;
return null;
}
internal Planet GetBottomRightNode2(Planet planet)
{
foreach (Planet gridPlanet in planets)
if (gridPlanet.X == planet.X + planetSize && gridPlanet.Y == planet.Y + yOffset)
return gridPlanet;
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解开递归
一个答案是解开你的递归。您会遇到堆栈溢出,因为递归级别太多,需要内存分配才能完成并返回,因此“堆栈溢出”。
限制递归
我过去为 XNA 游戏创建了一个编辑器,其中需要洪水填充算法,我所做的是对其递归次数设置上限退出前。实际上,这意味着我可能必须对未填充的剩余部分重新应用大的洪水填充,但这不会导致堆栈溢出。
洪水填充算法
这使用常规循环来避免堆栈溢出错误。
Unravel recursion
One answer is to unravel your recursion. You're getting a stack overflow because there is too many levels of recursion which requires memory allocation before it can finalize and return, thus "stack overflow".
Limit recursion
I have created an editor for an XNA game in the past where I've required a flood fill algorithm, what I did there was to put an upper limit on the number of times it'll recurse before exiting. Effectively this means that I may have to re apply a large flood fill to the remaining portions that wasn't filled, however it wouldn't cause a stack overflow.
Flood Fill Algorithms
This uses regular looping to avoid stack overflow errors.
避免递归(DetectHangingNeighbours 调用自身)。使用堆栈方法:
祝你好运。
Avoid recursion (DetectHangingNeighbours calling itself). Use instead a stack approach:
Good luck.