c# - 实现 Flood Fill 算法的变体。

标签 c# algorithm xna

我正在尝试使用 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/

相关文章:

c# - 如何将通用对象列表传递给方法?

排序数字的算法

简化 3d 表面的算法?

algorithm - 询问带IP核的FPGA设计

c# - 您如何阻止 SoundEffect 在 XNA windows 游戏关闭时崩溃?

Xna SpriteBatch Matrix.Decompose()

c# - 无法加载类型 'NHibernate.ByteCode.CaSTLe.ProxyFactoryFactory, NHibernate.ByteCode.CaSTLe'

c# - 使用 ExecuteReaderAsync 时出现死锁

c# - 从 ItemsControl 中的模板获取项目

c# - 我如何在 XNA 中找到旋转变换的 2D 坐标?