<p>An <code>n x n</code> grid is composed of <code>1 x 1</code> squares where each <code>1 x 1</code> square consists of a <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or blank space <code>&#39; &#39;</code>. These characters divide the square into contiguous regions.</p>

<p>Given the grid <code>grid</code> represented as a string array, return <em>the number of regions</em>.</p>

<p>Note that backslash characters are escaped, so a <code>&#39;\&#39;</code> is represented as <code>&#39;\\&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/1.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;/ &quot;]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/2.png" style="width: 200px; height: 198px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;  &quot;]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/4.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot;/\\&quot;,&quot;\\/&quot;]
<strong>Output:</strong> 5
<strong>Explanation: </strong>Recall that because \ characters are escaped, &quot;\\/&quot; refers to \/, and &quot;/\\&quot; refers to /\.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or <code>&#39; &#39;</code>.</li>
</ul>

class Solution:
    def regionsBySlashes(self, grid: List[str]) -> int:

        n = len(grid) * 3

        expanded = [[0 for i in range(n)] for i in range(n)]

        for gx in range(len(grid)):
            for gy in range(len(grid)):

                x = gx * 3
                y = gy * 3

                match grid[gx][gy]:
                    case "\\":
                        expanded[x][y] = 1
                        expanded[x + 1][y] = 0
                        expanded[x + 2][y] = 0
                        expanded[x][y + 1] = 0
                        expanded[x + 1][y + 1] = 1
                        expanded[x + 2][y + 1] = 0
                        expanded[x][y + 2] = 0
                        expanded[x + 1][y + 2] = 0
                        expanded[x + 2][y + 2] = 1
                    case "/":
                        expanded[x][y] = 0
                        expanded[x + 1][y] = 0
                        expanded[x + 2][y] = 1
                        expanded[x][y + 1] = 0
                        expanded[x + 1][y + 1] = 1
                        expanded[x + 2][y + 1] = 0
                        expanded[x][y + 2] = 1
                        expanded[x + 1][y + 2] = 0
                        expanded[x + 2][y + 2] = 0

        print(expanded)

        # build (undirected) graph

        nodes = [(x, y) for x in range(n) for y in range(n)]

        adj_list = {}

        for node in nodes:
            neighbors = []
            if node[0] < n - 1:
                neighbors.append((node[0] + 1, node[1]))
            if node[0] > 0:
                neighbors.append((node[0] - 1, node[1]))
            if node[1] < n - 1:
                neighbors.append((node[0], node[1] + 1))
            if node[1] > 0:
                neighbors.append((node[0], node[1] - 1))

            adj_list[node] = neighbors

        # repeating DFS to find number of graph components

        components = 0

        unexplored = set(nodes)
        explored = set()

        frontier = []

        while len(unexplored) > 0:
            head = random.choice(tuple(unexplored))

            # edge case of randomly starting on a border
            # note that when/how border grids are added to explored list doesn't matter
            # as long as they are counted properly
            if expanded[head[0]][head[1]] == 1:
                continue

            frontier = [head]

            while len(frontier) > 0:
                head = frontier.pop()
                explored.add(head)
                unexplored.discard(head)

                if expanded[head[0]][head[1]] == 1:
                    continue

                neighbors = adj_list[head]

                for neighbor in neighbors:
                    if neighbor not in explored:
                        frontier.append(neighbor)

            components += 1

        return components
21583