1. 程式人生 > >【leetcode】959. Regions Cut By Slashes

【leetcode】959. 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 ' '.

解題思路:“小樣,你以為穿個馬甲我就不認識你了”。如下圖,每個square有以下三種狀態,同時給這三種狀態定義如何轉換成3*3的矩陣,在矩陣中,連續的1表示斜槓。如果把grid中所有的square都進行矩陣轉換,那麼得到的將是一個由0和1組成的 3*len(grid) * 3*len(grid)的矩陣,這個題目就變成了 求島的數量  的題目。接下來就是DFS/BFS能做的事了。

 

程式碼如下:

class Solution(object):
    def regionsBySlashes(self, grid):
        """
        :type grid: List[str]
        :rtype: int
        """
        visit = []
        newGrid = []
        for i in grid:
            visit.append([0]*len(i)*3)
            visit.append([0] * len(i)*3)
            visit.append([0] * len(i) * 3)
            newGrid.append([0]*len(i)*3)
            newGrid.append([0] * len(i)*3)
            newGrid.append([0] * len(i) * 3)

        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j] == '/':
                    #newGrid[2*i][2*j+1] = newGrid[2*i+1][2*j] = 1
                    newGrid[3*i][3*j+2] = newGrid[3*i+1][3*j+1] = newGrid[3*i+2][3*j] = 1
                elif grid[i][j] == '\\':
                    #newGrid[2*i][2*j] = newGrid[2*i+1][2*j+1] = 1
                    newGrid[3*i][3*j] = newGrid[3*i + 1][3*j + 1] = newGrid[3*i+2][3*j+2] = 1

        direction = [(0,1),(0,-1),(1,0),(-1,0)]
        res = 0
        for i in range(len(newGrid)):
            for j in range(len(newGrid[i])):
                if visit[i][j] == 1 or newGrid[i][j] == 1:
                    continue
                queue = [(i,j)]
                visit[i][j] = 1
                res += 1
                while len(queue) > 0:
                    x,y = queue.pop(0)
                    #visit[x][y] = 1
                    for (x1,y1) in direction:
                        nextX = x + x1
                        nextY = y + y1
                        if nextX >= 0 and nextX < len(newGrid) and nextY >= 0 and nextY < len(newGrid)\
                                and newGrid[nextX][nextY] == 0 and visit[nextX][nextY] == 0:
                            visit[nextX][nextY] = 1
                            queue.append((nextX,nextY))
        return res