Swift - Slashes DFS
\n\nclass Solution {\n    func regionsBySlashes(_ grid: [String]) -> Int {\n        let n = grid.count\n        // Create a 3x3 grid for each cell in the origin
\n```\nclass Solution {\n    func regionsBySlashes(_ grid: [String]) -> Int {\n        let n = grid.count\n        // Create a 3x3 grid for each cell in the original grid to better represent the slashes and spaces.\n        var gridMap = Array(repeating: Array(repeating: -1, count: n * 3), count: n * 3)\n        \n        for i in 0..<n {\n            for j in 0..<n {\n                // Determine the character at the current position\n                let char = grid[i][grid[i].index(grid[i].startIndex, offsetBy: j)]\n                \n                // If the character is \'/\', mark the respective triangles\n                if char == "/" {\n                    gridMap[i * 3][j * 3 + 2] = 1    // Top-right part of the 3x3 grid\n                    gridMap[i * 3 + 1][j * 3 + 1] = 1  // Center part of the 3x3 grid\n                    gridMap[i * 3 + 2][j * 3] = 1    // Bottom-left part of the 3x3 grid\n                }\n                // If the character is \'\\\', mark the respective triangles\n                else if char == "\\\\" {\n                    gridMap[i * 3][j * 3] = 1        // Top-left part of the 3x3 grid\n                    gridMap[i * 3 + 1][j * 3 + 1] = 1  // Center part of the 3x3 grid\n                    gridMap[i * 3 + 2][j * 3 + 2] = 1  // Bottom-right part of the 3x3 grid\n                }\n            }\n        }\n        \n        // Helper function for DFS traversal\n        func dfs(_ x: Int, _ y: Int) {\n            // If the coordinates are out of bounds or the cell is already visited, return\n            if x < 0 || y < 0 || x >= n * 3 || y >= n * 3 || gridMap[x][y] != -1 {\n                return\n            }\n            // Mark the current cell as visited\n            gridMap[x][y] = 0\n            // Visit all neighboring cells\n            dfs(x + 1, y)\n            dfs(x - 1, y)\n            dfs(x, y + 1)\n            dfs(x, y - 1)\n        }\n        \n        var regions = 0\n        \n        // Traverse through the expanded grid\n        for i in 0..<n * 3 {\n            for j in 0..<n * 3 {\n                // If the current cell is unvisited, start a DFS which counts as a new region\n                if gridMap[i][j] == -1 {\n                    regions += 1\n                    dfs(i, j)\n                }\n            }\n        }\n        \n        // Return the total number of regions found\n        return regions\n    }\n}\n\n```
['Swift']