Easy | Clean Code | DFS | Video solution | C++
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n\nNow we have to Call DFS function for visiting all unvisted 0 by marking 1 and cou
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/3ce63303-1a6f-411b-88cb-46088debac69_1723269659.5740852.png)\n![image.png](https://assets.leetcode.com/users/images/011446b6-2798-444f-9836-f2723a29d3cf_1723269859.7391663.png)\n\nNow we have to Call DFS function for visiting all unvisted ```0``` by marking ```1``` and count the number of function all we actually do from our main function, this count will be our answer.\n# Video solution\n<!-- Describe your approach to solving the problem. -->\n[https://youtu.be/YdRwvbWolIw]()\n# Complexity\n- Time complexity: O(m*n*9)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(m*n*9)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\nvector<vector<int>> Back={{1,0,0},{0,1,0},{0,0,1}};\nvector<vector<int>> forward={{0,0,1},{0,1,0},{1,0,0}};\nvector<vector<int>> space={{0,0,0},{0,0,0},{0,0,0}};\nvector<int> x={0,0,1,-1};\nvector<int> y={1,-1,0,0};\n\n// used to replace every character with corresponding matrix\nvoid  ConvertTo01(vector<vector<int>>&v,int i,int j,int which){\n    if(which==0){\n        for(int ii= i;ii<i+3;ii++){\n            for(int jj=j;jj<j+3;jj++){\n                v[ii][jj]= space[ii-i][jj-j];\n            }\n        }\n    }\n    if(which==1){\n        for(int ii= i;ii<i+3;ii++){\n            for(int jj=j;jj<j+3;jj++){\n                v[ii][jj]= forward[ii-i][jj-j];\n            }\n        }\n    }\n    if(which==2){\n        for(int ii= i;ii<i+3;ii++){\n            for(int jj=j;jj<j+3;jj++){\n                v[ii][jj]= Back[ii-i][jj-j];\n            }\n        }\n    }\n}\n// Used to call convertTo01 function for each character\nvector<vector<int>> convert(vector<string>&grid){\n    int n= grid.size();\n    int m=grid[0].size();\n    vector<vector<int>>v(3*n,vector<int>(3*m));\n    for(int i=0;i<n;i++){\n        for(int j=0;j<m;j++){\n            if(grid[i][j]==\'/\') ConvertTo01(v,3*i,3*j,1);\n            else if(grid[i][j]==\'\\\\\') ConvertTo01(v,3*i,3*j,2);\n            else ConvertTo01(v,3*i,3*j,0);\n        }\n    }\n    return v;\n}\n//check is this point on the grid or not?\nbool IsValid(vector<vector<int>>&v,int i,int j){\n    if(i<0 || j<0 || i>=v.size() || j>=v[0].size() || v[i][j]==1) return false;\n    return true;\n}\n// visite all unvisited cell in a particular region\nvoid Dfs(vector<vector<int>>&v,int i,int j){\n    if(v[i][j]==1) return;\n    v[i][j]=1;\n    for(int k=0;k<4;k++){\n        int newx= i+x[k];\n        int newy= j+y[k];\n        if(IsValid(v,newx,newy)){\n            Dfs(v,newx,newy);\n        }\n    }\n}\n\n    int regionsBySlashes(vector<string>& grid) {\n        int ans=0;\n        int n=grid.size();\n        int m=grid[0].size();\n       vector<vector<int>> v= convert(grid);\n        for(int i=0;i<3*n;i++){\n             for(int j=0;j<3*m;j++){\n                if(v[i][j]==0){ // Dfs only if cell is unvisited\n                    Dfs(v,i,j);\n                    ans++; // count the number of region\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']