Union Find splitting a cell into 3 * 3 grid || C++
Inspired by votrubac\'s brilliant idea of splitting each cell into 3 x 3 grid, following is the code using Union-find solution which is same as counting number 
Inspired by votrubac\'s [brilliant idea](https://leetcode.com/problems/regions-cut-by-slashes/discuss/205674/C%2B%2B-with-picture-DFS-on-upscaled-grid) of splitting each cell into 3 x 3 grid, following is the code using Union-find solution which is same as counting number of islands. We have represented a slash as a diagonal of 3 \'1\'s, while empty cell has been represented by 0\'s. \n\nAlso, by default, parent of a square containing 1 is -1, while parent of square containing 0 is the square itself.\n```\nclass Solution {\npublic:\n    int islands;\n    \n    int find(int x, vector<int>& parent){\n        if(parent[x] == -1)\n            return -1;\n        if(parent[x] != x)\n            parent[x] = find(parent[x], parent);\n        return parent[x];\n    }\n    \n    void uni(int x , int y , vector<int>& parent){\n        x = find(x, parent), y = find(y, parent);\n        if(y == -1) return;\n        if(x != y){\n            islands-= 1;\n            parent[y] = x;\n        }       \n        \n    }\n        \n    int regionsBySlashes(vector<string>& grid) {\n        \n        int n = grid.size();\n        islands = 9 * n * n;\n        vector<vector<int>> graph(n * 3, vector<int>(n * 3 , 0));\n        \n        for(int i = 0; i < n; i++){\n            for(int j = 0; j < n ; j++){\n                if(grid[i][j] == \'/\')\n                    graph[i * 3][j * 3 + 2] = graph[i * 3 + 1][j * 3 + 1] = graph[i * 3 + 2][j * 3] = 1, islands -= 3;\n                if(grid[i][j] == \'\\\\\')\n                    graph[i * 3][j * 3] = graph[i * 3 + 1][j * 3 + 1] = graph[i * 3 + 2][j * 3 + 2] = 1, islands -= 3;\n            }\n        }\n        \n        vector<int> parent(9 * n * n, -1);\n        \n        for(int i = 0; i < 3 * n; i++){\n            for(int j = 0; j < 3 * n ; j++){\n                int index = i * 3 * n + j;\n                if(graph[i][j] == 0)\n                    parent[index] = index;\n            }\n        }\n        \n        for(int i = 0; i < 3 * n; i++){\n            for(int j = 0; j < 3 * n ; j++){\n                int index = i * 3 * n + j;\n                if(graph[i][j] == 0){\n                    if(i > 0)\n                    uni(index , index - 3 * n, parent);\n                    if(j > 0)\n                    uni(index , index - 1, parent);\n                }\n            }\n        }\n\n        return islands;        \n    }\n};\n```
['Union Find']