Example #1
0
    public int MaxAreaOfIsland(int[,] grid)
    {
        int l1 = grid.GetLength(0);
        int l2 = grid.GetLength(1);

        MySet[,] sets = new MySet[l1, l2];
        int max = 0;

        for (int i = 0; i < l1; ++i)
        {
            for (int j = 0; j < l2; ++j)
            {
                if (grid[i, j] == 0)
                {
                    continue;
                }

                MySet cur = null;
                cur        = new MySet();
                sets[i, j] = cur;

                if (i > 0)
                {
                    cur = cur.Merge(sets[i - 1, j]);
                }
                if (j > 0)
                {
                    cur = cur.Merge(sets[i, j - 1]);
                }

                if (cur.Size > max)
                {
                    max = cur.Size;
                }
            }
        }

        return(max);
    }
        public void TestSetMerge()
        {
            MySet myset1 = new MySet(SimpleBitmap.BitmapOf);

            myset1.Add(new Node()
            {
                Name = "a", Number = 1
            });
            myset1.Add(new Node()
            {
                Name = "a", Number = 3
            });

            MySet myset2 = new MySet(SimpleBitmap.BitmapOf);

            myset2.Add(new Node()
            {
                Name = "a", Number = 2
            });
            myset2.Add(new Node()
            {
                Name = "b", Number = 1
            });


            MySet myset3 = MySet.Merge(myset1, myset2, SimpleBitmap.BitmapOf);

            Assert.IsTrue(myset3.Contains(new Node()
            {
                Name = "a", Number = 1
            }));
            Assert.IsTrue(myset3.Contains(new Node()
            {
                Name = "a", Number = 2
            }));
            Assert.IsTrue(myset3.Contains(new Node()
            {
                Name = "a", Number = 3
            }));
            Assert.IsTrue(myset3.Contains(new Node()
            {
                Name = "b", Number = 1
            }));
        }