【LeetCode】959. Regions Cut By Slashes 解题报告(Python)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fuxuemingzhu/article/details/85039057

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/regions-cut-by-slashes/description/

题目描述

In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. These characters divide the square into contiguous regions.

(Note that backslash characters are escaped, so a \ is represented as "\\".)

Return the number of regions.

Example 1:

Input:
[
  " /",
  "/ "
]
Output: 2
Explanation: The 2x2 grid is as follows:

此处输入图片的描述

Example 2:

Input:
[
  " /",
  "  "
]
Output: 1
Explanation: The 2x2 grid is as follows:

此处输入图片的描述

Example 3:

Input:
[
  "\\/",
  "/\\"
]
Output: 4
Explanation: (Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.)
The 2x2 grid is as follows:

此处输入图片的描述

Example 4:

Input:
[
  "/\\",
  "\\/"
]
Output: 5
Explanation: (Recall that because \ characters are escaped, "/\\" refers to /\, and "\\/" refers to \/.)
The 2x2 grid is as follows:

此处输入图片的描述

Example 5:

Input:
[
  "//",
  "/ "
]
Output: 3
Explanation: The 2x2 grid is as follows:

此处输入图片的描述

Note:

  1. 1 <= grid.length == grid[0].length <= 30
  2. grid[i][j] is either '/', '\', or ' '.

题目大意

有个N*N的格子,每个格子有三种状态,左划线,右划线,没有线。给出了每个格子的状态之后,求最后这些线能把格子分割成多少个不同的块。

解题方法

这个题给的例子虽然都是22的,但是事实上是NN的,需要注意下。这个题其实没那么难,它主要是给定了切割的方式,问我们切割之后还有多少个联通的区域。

既然是问联通的方案,那么我们就根据划定的范围来知道哪些是联通的。具体做法当然就是并查集。

--------
| \ 0 / |
| 3\ /  |
|  /\ 1 |
|/  2 \ |
|-------|

像上面这样划分把每一个格子进行4块划分,从上面开始顺时针编号为0,1,2,3. 并查集的使用方式是,如果两个格子相邻,那么就把他们格子的相邻编号合并到一起。然后根据格子自身的画线方式,再把自身的不同区域合并到一起。

显而易见,每行的左边各自的1和右边的那个3是相邻的,每列的上边的2和下边的2是相邻的。自身的格子划分方式是:如果是'/',那么0和3,1和2分别相邻;如果格子划分方式是'\\',那么0和1,3和2分别相邻。

最后统计不同的区域有多少,想一下,如果都不相邻,那么有N*N个区域,但是每次合并都会造成少了一个区域。所以直接在合并的时候减去区域即可。

代码如下:

class Solution(object):
    def regionsBySlashes(self, grid):
        """
        :type grid: List[str]
        :rtype: int
        """
        self.N = len(grid)
        m_ = range(self.N * self.N * 4)
        self.count = self.N * self.N * 4
        for r in range(self.N):
            line = grid[r]
            for c in range(self.N):
                w = line[c]
                if r > 0:
                    self.u(m_, self.g(r - 1, c, 2), self.g(r, c, 0))
                if c > 0:
                    self.u(m_, self.g(r, c - 1, 1), self.g(r, c, 3))
                if w != '/':
                    self.u(m_, self.g(r, c, 0), self.g(r, c, 1))
                    self.u(m_, self.g(r, c, 3), self.g(r, c, 2))
                if w != '\\':
                    self.u(m_, self.g(r, c, 0), self.g(r, c, 3))
                    self.u(m_, self.g(r, c, 1), self.g(r, c, 2))
        return self.count

    def f(self, m_, a):
        if m_[a] == a:
            return a
        return self.f(m_, m_[a])
    
    def u(self, m_, a, b):
        pa = self.f(m_, a)
        pb = self.f(m_, b)
        if (pa == pb):
            return
        m_[pa] = pb
        self.count -= 1
    
    def g(self, r, c, i):
        return (r * self.N + c) * 4 + i

参考资料:https://www.youtube.com/watch?v=Mia50ouW1T4

日期

2018 年 12 月 16 日 —— 周赛好难

猜你喜欢

转载自blog.csdn.net/fuxuemingzhu/article/details/85039057