Different approach with picture
\n\nPrerequisite: DSU\n\nThis problem can be solved with Union Find Disjoint Set.\n1. First we need to define the elements(nodes of graph) of the set. If we con
![image](https://assets.leetcode.com/users/c_jain/image_1577826948.png)\n\nPrerequisite: [DSU](https://cp-algorithms.com/data_structures/disjoint_set_union.html)\n\nThis problem can be solved with Union Find Disjoint Set.\n1. First we need to define the elements(nodes of graph) of the set. If we consider grid\'s cells as node then it will be not possible to find appropriate regions through DSU.\n2. We need to express/imagine our grid differently.\n3. For example : this `[" /"," /"]`  2 x 2 grid can be break as 3 x 3 grid as shown in the picture.\n4. That\'s all, just traverse the input grid and whenever we encounter `\'/\' or  \'\\\\\'` then consider it as a edge connecting nodes in the new grid.\n5. If they are of different set then Union them otherwise increase the answer by 1 because we are trying to union 2 nodes which were already in the same set, it means if we put a edge betweem them then this will divide the region in 2 parts.\n\n```c++\n// C++ code\nclass Solution {\npublic:\n    \n    int find(int a, int *parent) {\n        if(a == parent[a]) return a;\n        else return parent[a] = find(parent[a], parent);\n    }\n    \n    void Union(int a, int b, int *parent, int *size) {\n        a = find(a,parent);\n        b = find(b,parent);\n        \n        if(size[a] < size[b]) swap(a,b);\n        parent[b] = a;\n    }\n    \n\t// Info -> new grid is of size n+1 as shown in the picture above\n    int regionsBySlashes(vector<string>& grid) {\n        \n        int n = grid.size();\n\t\t// In the new grid there will be total of (n+1)*(n+1) nodes\n\t\t// as you can see in the picture above\n        int *parent = new int[(n+1)*(n+1)];\n        int *size = new int[(n+1)*(n+1)]; // union by size\n        int ans = 1;\n        \n        //Building sets\n        for(int i=0; i<=n; i++)\n            for(int j=0; j<=n; j++) {\n                \n                // A 2D cell can be convert to a 1D cell\n\t\t\t\t// like this: row_number * total_columns + column_number\n\t\t\t\tint node = i*(n+1) + j;\n                parent[node] = node;\n                size[node] = 1;\n            }\n        \n        //Merging boundary nodes\n\t\t//Initially all the boundary nodes of the new grid are connected.\n\t\t// So we put them in the same set and they are surrounding 1 region at this moment.\n\t\t//Regions will increase as we traverse the input grid.\n        for(int i=0; i<n; i++) {\n            \n            Union(0*(n+1)+i, 0*(n+1)+i+1, parent, size); // top\n            Union(n*(n+1)+i, n*(n+1)+i+1, parent, size); // bottom\n            Union(i*(n+1)+0, (i+1)*(n+1)+0, parent, size); // left\n            Union(i*(n+1)+n, (i+1)*(n+1)+n, parent, size); // right\n        }\n        \n        // Everything is set, time to process our input grid :)\n        for(int i=0; i<n; i++)\n            for(int j=0; j<n; j++) {\n                \n                int node1, node2;\n                \n                if(grid[i][j] == \' \') continue;\n                \n\t\t\t\t//here n+1 is new grid\'s column and row length\n\t\t\t\t// this \'\\\' character will connect nodes present in "ith and (i+1)th rows" \n\t\t\t\t// and "jth and (j+1)th columns" respectively in new grid\n                if(grid[i][j] == \'\\\\\') \n                    node1 = i*(n+1)+j, node2 = (i+1)*(n+1)+(j+1);\n\t\t\t\t// this \'/\' character will connect nodes present in "ith and (i+1)th rows" \n\t\t\t\t// and "(j+1)th and jth columns" respectively in new grid\n                else if(grid[i][j] == \'/\')\n                    node1 = i*(n+1)+(j+1), node2 = (i+1)*(n+1)+j;\n                \n\t\t\t\t// Already in the same set -> increase answer\n                if(find(node1, parent) == find(node2, parent)) ans++;\n\t\t\t\t// Connect them \n                else Union(node1, node2, parent, size);\n            }\n        \n        return ans;\n    }\n};\n```
[]