1. 程式人生 > >[Swift Weekly Contest 115]LeetCode959. 由斜槓劃分區域 | Regions Cut By Slashes

[Swift Weekly Contest 115]LeetCode959. 由斜槓劃分區域 | Regions Cut By Slashes

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 ' '.

在由 1 x 1 方格組成的 N x N 網格 grid 中,每個 1 x 1 方塊由 /\ 或空格構成。這些字元會將方塊劃分為一些共邊的區域。

(請注意,反斜槓字元是轉義的,因此 \ 用 "\\" 表示。)。

返回區域的數目。

示例 1:

輸入:
[
  " /",
  "/ "
]
輸出:2
解釋:2x2 網格如下:

示例 2:

輸入:
[
  " /",
  "  "
]
輸出:1
解釋:2x2 網格如下:

示例 3:

輸入:
[
  "\\/",
  "/\\"
]
輸出:4
解釋:(回想一下,因為 \ 字元是轉義的,所以 "\\/" 表示 \/,而 "/\\" 表示 /\。)
2x2 網格如下:

示例 4:

輸入:
[
  "/\\",
  "\\/"
]
輸出:5
解釋:(回想一下,因為 \ 字元是轉義的,所以 "/\\" 表示 /\,而 "\\/" 表示 \/。)
2x2 網格如下:

示例 5:

輸入:
[
  "//",
  "/ "
]
輸出:3
解釋:2x2 網格如下:

提示:

  1. 1 <= grid.length == grid[0].length <= 30
  2. grid[i][j] 是 '/''\'、或 ' '

104ms
 1 class Solution {
 2     let TOP = 0, LEFT = 1, RIGHT = 2, BOTTOM = 3
 3     
 4     func regionsBySlashes(_ grid: [String]) -> Int {
 5         var len:Int = grid.count
 6         //定義三維陣列,元素初始化為0,由內往外
 7         var g = [[[Int]]](repeating: [[Int]](repeating: [Int](repeating: 0, count: 4), count: len), count: len)
 8         var n:Int = 0
 9         for i in 0..<len
10         {
11             for j in 0..<len
12             {
13                 for k in 0..<4
14                 {
15                     if g[i][j][k] == 0
16                     {
17                         n += 1
18                         mark(grid, &g, i, j, k, n) 
19                     }
20                 }
21             }
22         }
23         return n
24     }
25     
26     func mark(_ grid:[String],_ g: inout [[[Int]]],_ i:Int,_ j:Int,_ k:Int,_ n: Int)
27     {
28         var len:Int = g.count
29         if i < 0 || i >= len || j < 0 || j >= len || g[i][j][k] != 0 {return}
30         g[i][j][k] = n
31         var c:Character = grid[i][j]
32         if c == "/"
33         {
34             if k == LEFT || k == TOP
35             {
36                 mark(grid, &g, i, j, LEFT, n)
37                 mark(grid, &g, i, j, TOP, n)
38             }
39             else
40             {
41                 mark(grid, &g, i, j, BOTTOM, n)
42                 mark(grid, &g, i, j, RIGHT, n)
43             }
44         }
45         else if c == "\\"
46         {
47             if k == LEFT || k == BOTTOM
48             {
49                 mark(grid, &g, i, j, LEFT, n)
50                 mark(grid, &g, i, j, BOTTOM, n)
51             }
52             else
53             {
54                 mark(grid, &g, i, j, TOP, n)
55                 mark(grid, &g, i, j, RIGHT, n)
56             }
57         }
58         else
59         {
60             mark(grid, &g, i, j, TOP, n)
61             mark(grid, &g, i, j, LEFT, n)
62             mark(grid, &g, i, j, RIGHT, n)
63             mark(grid, &g, i, j, BOTTOM, n)
64         }
65         
66         if k == LEFT
67         {
68             mark(grid, &g, i, j - 1, RIGHT, n)
69         }
70         else if k == RIGHT
71         {
72             mark(grid, &g, i, j + 1, LEFT, n)
73         }
74         else if k == BOTTOM
75         {
76            mark(grid, &g, i + 1, j, TOP, n)
77         }
78         else if k == TOP
79         {
80             mark(grid, &g, i - 1, j, BOTTOM, n)
81         }
82     }
83 }
84 
85 extension String {        
86     //subscript函式可以檢索陣列中的值
87     //直接按照索引方式擷取指定索引的字元
88     subscript (_ i: Int) -> Character {
89         //讀取字元
90         get {return self[index(startIndex, offsetBy: i)]}
91     }
92 }