毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> 嵌入式 >> 正文

输入一个二维随机bool数组B[,],求出全部连通域(8连接) 第3页

更新时间:2012-6-30:  来源:毕业论文
namespace CSharpTest
{
    class Program
    {
        struct Point
        {
            public int X;
            public int Y;
            public Point(int x, int y)
            { X = x; Y = y; }
        }

        class Graph
        {
            public readonly int Width;
            public readonly int Height;

            public bool[,] BitMap;

            public List<List<Point>> Fill()
            {                           
                bool[,] history = new bool[Width, Height];
                List<List<Point>> result = new List<List<Point>>();

                for (int i = 0; i < Width; i++)
                {
                    for (int j = 0; j < Height; j++)
                    {
                        if (BitMap[i, j] && !history[i, j])
                            result.Add(BFS(i, j, history));
                    }
                }

                return result;
            }

            private List<Point> BFS(int x, int y, bool[,] history)
            {
                List<Point> currentPoints = new List<Point>();
                Queue<Point> queue = new Queue<Point>();
                Point cPoint = new Point(x, y);
                queue.Enqueue(cPoint);

                while (queue.Count > 0)
                {
                    Point current = queue.Dequeue();
                    currentPoints.Add(current);
                    history[current.X, current.Y] = true;

                    for (int i = current.X - 1; i <= current.X + 1; i++)
                    {
                        for (int j = current.Y - 1; j <= current.Y + 1; j++)
                        {
                            if (i == current.X && j == current.Y)
                                continue;

                            if (Check(i, j) && !history[i,j])
                                queue.Enqueue(new Point(i, j));
                        }
                    }
                }

                return currentPoints;
            }

            private bool Check(int x, int y)
            {
                if (x < 0 || x >= Width || y < 0 || y >= Height)
                    return false;

                return BitMap[x, y];
            }

            public Graph(int width, int height)

上一页  [1] [2] [3] [4] 下一页

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。