[Python] Visual Explanation
python\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n\n        N = len(grid)\n        \n        #1. Make a list of all the regions 
```python\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n\n        N = len(grid)\n        \n        #1. Make a list of all the regions (nodes) that are connected\n        even_edges = [[4*i+2,4*(i+1)] for i in range(N*N) if (4*(i+1) < 4*N*N)&((4*i+2)//(4*N) == 4*(i+1)//(4*N))]\n        odd_edges = [[4*i-1,4*(i+N)-3] for i in range(1,N*N+1) if (0 < 4*i-1 < 4*(i+N)-3 < 4*N*N)]\n        edges = even_edges + odd_edges\n        \n        for r in range(N):\n            for c in range(N):\n                b = r*N + c #box that we are in ([[0,1,2],[3,4,5],[6,7,8]])\n                base = 4*b\n                if grid[r][c] == \' \':\n                    for j in range(4):\n                        edges.append([base+j,base+j+1 if (j!=3) else base])\n                elif grid[r][c] == \'\\\\\':\n                    edges.extend([[base+1,base+2],[base,base+3]])\n                elif grid[r][c] == \'/\':\n                    edges.extend([[base,base+1],[base+2,base+3]])\n        \n        #2. Group nodes together by their edges\n        uf = UnionFind()\n        for a, b in edges:\n            uf.union(a, b)\n        \n        return len(uf.groups)\n    \nclass UnionFind(object):\n    def __init__(self):\n        self.group_id = 0\n        self.groups = {}\n        self.node_id = {}\n    \n    def union(self, a, b):\n        \'\'\'There is an edge between node a and node b.  Update the groups of nodes to reflect this.\'\'\'\n        A, B = a in self.node_id, b in self.node_id\n        if A and B and self.node_id[a] != self.node_id[b]:\n            self.merge(a, b)\n        elif A or B:\n            self.add(a, b)\n        else:\n            self.create(a, b)\n    \n    def merge(self, a, b):\n        \'\'\'Node a and node b belong to different groups.  Merge the smaller group with the larger group.\'\'\'\n        obs, targ = sorted((self.node_id[a], self.node_id[b]), key = lambda id: len(self.groups[id]))\n        for node in self.groups[obs]:\n            self.node_id[node] = targ\n        self.groups[targ] |= self.groups[obs]\n        del self.groups[obs]\n        \n    def add(self, a, b):\n        \'\'\'One node a or b belongs to a group.  Add the new node to the existing group.\'\'\'\n        a, b = (a, b) if a in self.node_id else (b, a)\n        targ = self.node_id[a]\n        self.node_id[b] = targ\n        self.groups[targ] |= set([b])\n    \n    def create(self, a, b):\n        \'\'\'Neither node a or node b belongs to a group.  Create a new group consisting of {a, b}.\'\'\'\n        self.groups[self.group_id] = set([a,b])\n        self.node_id[a] = self.node_id[b] = self.group_id\n        self.group_id += 1\n```\n\n```html5\n<i>Props to <a href="https://leetcode.com/datou12138/">@datau12138</a> whose comment insipired this solution.</i>\n```\n\nThe tricky part of this problem is turning the input into something that we can work with.  I will do my best to explain how to do this through an example with visuals.  We will use the first example in the description:\n\n```\n#i.e. a 2 by 2 square (N = 2)\ninput = [" /", "/ "]\n```\n\nFirst divide the four squares like so:\n\n<img src = "https://assets.leetcode.com/users/images/2b4fb56a-7116-4747-8e93-a5e1d62f8f09_1597175092.4666784.png" width="60%"><img src ="https://assets.leetcode.com/users/images/732cb23e-738c-401c-9421-f55d32574058_1597176019.3388517.png">\n\n\nIn the middle we have the box id and on the left each box is split into four sections numbered from ```4*b to 4*(b+1)```. We will treat each section as a node in our graph.  On the right is how the box will look after it is slashed.\n\n```html5\n<b>Important Note:</b>  [2,4], [10,12], [3,9], and [7,13] will <b>always</b> be connected because a slash cannot separate them.  \n```\n\nThus, the step 1.1 is to find the edges that are guaranteed to be connected.  These are the edges that connect **one box to another.**  ```even_edges``` refers to [2,4] and [10,12] and ```odd_edges``` refers to [3,9] and [7,13].  \n\n```python\n# So far we have this:\nedges = [[2, 4], [10, 12], [3, 9], [7, 13]]\n```\n\nNow, step 1.2 is to find which sections are connected by an edge **within each box**.\n\n```python\n# Which gives us the following edges:\nedges = [[2, 4], [10, 12], [3, 9], [7, 13],      #connecting boxes\n         [0, 1], [1, 2], [2, 3], [3, 0],         #box 0\n         [4, 5], [6, 7],                         #box 1\n         [8, 9], [10, 11],                       #box 2\n         [12, 13], [13, 14], [14, 15], [15, 12]] #box 3\n```\n\nNotice that boxes 1 and 2 are missing [8,11], [9,10], [4,7], [5,16] because these sections were split by a slash.  \n\nNow that we have all of the edges, all that is left to do is to collect nodes into groups that are connected by a network of edges.  The annotations in step 2 explain the Group by ID process that is used for this, which I think is a more intuitive method than union-find.  \n\nThis results in the following groups:\n```python\ngroup = {0: {0, 1, 2,  3,  4,  5,  8,  9},  #top half\n         1: {6, 7, 10, 11, 12, 13, 14, 15}} #bottom half\n```\n\nThe final answer is the number of groups that we have (```2```).  \n\nHope this helps! Feel free to ask questions.
[]