【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

猜你喜欢

转载自www.cnblogs.com/seyjs/p/10136919.html