C++ 3x3 Pixel(Grid) & Union-Find(Whiteboard Explanation with images)
\n\nPixel Approach Implementation\n\nclass Solution {\npublic:\n    void islandcolor(int i,int j,vector<vector<int>> &island,int n)\n    {\n        if(i<0 or i>
![image](https://assets.leetcode.com/users/images/54557c0f-950a-4505-805a-97dbb50fcbc5_1601304835.7116442.png)\n\nPixel Approach Implementation\n```\nclass Solution {\npublic:\n    void islandcolor(int i,int j,vector<vector<int>> &island,int n)\n    {\n        if(i<0 or i>=n or j<0 or j>=n or island[i][j]==0)\n            return;\n        island[i][j]=0;\n        islandcolor(i+1,j,island,n);\n        islandcolor(i,j+1,island,n);\n        islandcolor(i-1,j,island,n);\n        islandcolor(i,j-1,island,n);\n    }\n    int regionsBySlashes(vector<string>& grid) {\n        int n=grid.size();\n        vector<vector<int>> island(3*n,vector<int> (3*n,1));\n        for(int i=0;i<n;i++)\n        {\n            int row=i*3;\n            int top=0,j=0;\n            while(top<grid[i].length())\n            {\n                if(grid[i][top]==\'/\')\n                {\n                    int x=row;\n                    int y=j+2;int count=3;\n                    while(count--)\n                    {\n                        island[x][y]=0;\n                        x++;\n                        y--;\n                    }\n                }\n                else if(grid[i][top]==\'\\\\\')\n                {\n                    int x=row;\n                    int y=j;int count=3;\n                    while(count--)\n                    {\n                        island[x][y]=0;\n                        x++;\n                        y++;\n                    }\n                }\n                top++;\n                j+=3;\n            }\n        }\n        int ans=0;\n        for(int i=0;i<3*n;i++)\n        {\n            for(int j=0;j<3*n;j++)\n            {\n                if(island[i][j]!=0)\n                {\n                    ans++;\n                    islandcolor(i,j,island,3*n);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n![image](https://assets.leetcode.com/users/images/ef4df216-29ef-4608-a293-7a9ef6107714_1601304924.3709579.png)\nUnion Find Implementation\n```\nclass Solution {\npublic:\n    int find(int array[],int x)\n    {\n        if(array[x]==x)\n            return x;\n        return find(array,array[x]);\n    }\n    void unionds(int array[],int x,int y)\n    {\n        int xf=find(array,x);\n        int yf=find(array,y);\n        array[xf]=yf;\n    }\n    int regionsBySlashes(vector<string>& grid) {\n        int n=grid.size();\n        int parent[4*n*n];\n        for(int i=0;i<4*n*n;i++)parent[i]=i;\n        for(int row=0;row<n;row++)\n        {\n            int start=0;\n            for(int col=0;col<n;col++)\n            {\n                int base=4*n*row + 4*col;\n\n                if(grid[row][start]!=\'/\')\n                   {\n                       unionds(parent,base,base+2);\n                       unionds(parent,base+1,base+3);\n\n\n                   }\n                if(grid[row][start]!=\'\\\\\')\n                {\n                    unionds(parent,base,base+1);\n                    unionds(parent,base+2,base+3);\n                }\n                start++;\n                if(col<n-1)\n                    unionds(parent,base+2,base+5);\n                if(row<n-1)\n                    unionds(parent,base+3,4*n*(row+1)+4*col);\n            }\n        }\n        int ans=0;\n        for(int i=0;i<4*n*n;i++)\n        {\n            if(parent[i]==i)\n                ans++;\n        }\n        return ans;\n    }\n};\n```\n
['Union Find']