C++ || Easy to Understand || Islands || DFS
In this question, the fastest and best way is to use Union Find method. However, the concept of UF is quite hard to me and I am not going to practice now. There
In this question, the fastest and best way is to use Union Find method. However, the concept of UF is quite hard to me and I am not going to practice now. Therefore, I used the particular one and user-friendly method to solve it question:\n\n1. think about the grid like number of islands. for a nxn size of grid, we just deem it as a (3*n)(3*n) square with 1. \n2. for \'/\' or \'\\\\\', just let 0 be the boarder of the island.\n3. After initializing the board, we just use dfs to find the boarder of the board.\n4. remember return the visited node to 0 so that we can avoid the repeated visit.\n5. In the dfs function, we need check whether the positions of row and column are inside the board.\n6.  for each time, we go to 4 dimensions from the node.\n\nThat is my idea about this question.\n\n```\nclass Solution {\npublic:\n    \n    void dfs(vector<vector<int>>& island, int i, int j){\n        // make sure inside the board\n        if(i<0 || j<0 || i>=island.size() || j>=island[i].size()) return;\n        \n        if(island[i][j] != 1) return;\n        island[i][j] = 0;\n        // for each time, we go to 4 dimensions\n        dfs(island, i+1, j);\n        dfs(island, i-1, j);\n        dfs(island, i, j+1);\n        dfs(island, i, j-1);\n    }\n    \n    int regionsBySlashes(vector<string>& grid) {\n        int n = grid.size();\n        vector<vector<int>> island(n*3, vector<int>(n*3, 1));\n        // initialize the board \n        // for example, grid = ["/\\\\","\\\\/"], the island will be :\n        /*\n            1 1 0 0 1 1\n            1 0 1 1 0 1\n            0 1 1 1 1 0\n            0 1 1 1 1 0\n            1 0 1 1 0 1\n            1 1 0 0 1 1\n        */\n        for(int i=0; i<n; i++){\n            for(int j=0; j<n; j++){\n                if(grid[i][j] == \'/\'){\n                    island[i*3 + 2][j*3] = island[i*3 + 1][j*3 + 1] = island[i*3][j*3 + 2] = 0;\n                }else if (grid[i][j] == \'\\\\\'){\n                    island[i*3][j*3] = island[i*3 + 1][j*3 + 1] = island[i*3 + 2][j*3+ 2] = 0;\n                }\n            }\n        }\n        int regions = 0;\n        // use dfs method to visit each node, return each visited node to 0.\n        for(int i=0; i<n*3; i++){\n            for(int j=0; j<n*3; j++){\n                if(island[i][j] == 1){\n                    regions ++;\n                    dfs(island, i, j);\n                }\n            }\n        }\n        return regions;\n    }\n};\n```
[]