Пример #1
0
        // open site (row i, column j) if it is not already
        public void open(int i, int j)
        {
            validateIndexes(i, j);
            if (isOpen(i, j))
            {
                return;
            }

            percolationGrid[i - 1, j - 1] = true;

            int currentValueForQU = getValueForQU(i, j);

            if (i != 1 && isOpen(i - 1, j))
            {
                weightedQuickUnionUF.union(currentValueForQU, getValueForQU(i - 1, j));
                weightedQuickUnionUFForFull.union(currentValueForQU, getValueForQU(i - 1, j));
            }

            if (j != gridDimension && isOpen(i, j + 1))
            {
                weightedQuickUnionUF.union(currentValueForQU, getValueForQU(i, j + 1));
                weightedQuickUnionUFForFull.union(currentValueForQU, getValueForQU(i, j + 1));
            }

            if (i != gridDimension && isOpen(i + 1, j))
            {
                weightedQuickUnionUF.union(currentValueForQU, getValueForQU(i + 1, j));
                weightedQuickUnionUFForFull.union(currentValueForQU, getValueForQU(i + 1, j));
            }

            if (j != 1 && isOpen(i, j - 1))
            {
                weightedQuickUnionUF.union(currentValueForQU, getValueForQU(i, j - 1));
                weightedQuickUnionUFForFull.union(currentValueForQU, getValueForQU(i, j - 1));
            }

            if (i == 1)
            {
                weightedQuickUnionUF.union(currentValueForQU, virtualTop);
                weightedQuickUnionUFForFull.union(currentValueForQU, virtualTop);
            }

            if (i == gridDimension)
            {
                weightedQuickUnionUF.union(currentValueForQU, virtualBottom);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            WeightedQuickUnion qu = new WeightedQuickUnion(10);

            qu.union(4, 3);
            qu.union(3, 8);
            qu.union(6, 5);
            qu.union(9, 4);
            qu.union(2, 1);
            qu.union(5, 0);
            qu.union(7, 2);
            qu.union(6, 1);
            qu.union(7, 3);



            qu.Print();
        }