1. 程式人生 > >【LeetCode】959. Regions Cut By Slashes 解題報告(Python)

【LeetCode】959. Regions Cut By Slashes 解題報告(Python)

作者: 負雪明燭
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 日 —— 周賽好難