我正在尝试使用 flood fill算法在列表中找到所有相似的相邻对象,并将它们标记为删除。我试图改编维基百科上的伪代码,但卡住了。
列表中的每个对象都有一个 int X 值、一个 int Y 值、一个 Name 和一个 bool 来标记为删除。我想匹配名字。
程序在没有 try-catch 的情况下挂起,并随它退出。它不会返回错误消息。这是我目前所拥有的,试图在正上方找到任何物体。
//Find neighbouring bubbles
gameGrid.DetectNeighbours2(gameGrid.planets.Last(), gameGrid.planets.Last().name);
//Flood fill algorithm to detect all connected planets
internal void DetectNeighbours(Planet p, Planet.Name planetName)
{
try
{
if (p.planet != planetName)
return;
p.deletable = true;
DetectNeighbours(GetTopNode(p), planetName);
}
catch (Exception err)
{
Debug.WriteLine(err.Message);
}
}
internal Planet GetTopNode(Planet b)
{
foreach (Planet gridPlanet in planets)
{
if (gridPlanet .Y == b.Y - 50)
return gridPlanet ;
}
return b; //Don't think this is right, but couldn't think of alternative
}
最佳答案
或者你可以这样重写它。
gameGrid.DetectNeighbours2(gameGrid.planets.Last());
//Flood fill algorithm to detect all connected planets
internal void DetectNeighbours(Planet p)
{
try
{
if (p == null || p.deletable)
return;
p.deletable = true;
DetectNeighbours(GetTopNode(p));
}
catch (Exception err)
{
Debug.WriteLine(err.Message);
}
}
internal Planet GetTopNode(Planet b)
{
foreach (Planet gridPlanet in planets)
{
if (gridPlanet .Y == b.Y - 50)
return gridPlanet ;
}
return null;
}
关于c# - 实现 Flood Fill 算法的变体。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6860060/