【leetcode】1034. Coloring A Border

题目如下:

Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid square at that location.

Two squares belong to the same connected component if and only if they have the same color and are next to each other in any of the 4 directions.

The border of a connected component is all the squares in the connected component that are either 4-directionally adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).

Given a square at location (r0, c0) in the grid and a color, color the border of the connected component of that square with the given color, and return the final grid.

 

Example 1:

Input: grid = [[1,1],[1,2]], r0 = 0, c0 = 0, color = 3
Output: [[3, 3], [3, 2]]

Example 2:

Input: grid = [[1,2,2],[2,3,2]], r0 = 0, c0 = 1, color = 3
Output: [[1, 3, 3], [2, 3, 3]]

Example 3:

Input: grid = [[1,1,1],[1,1,1],[1,1,1]], r0 = 1, c0 = 1, color = 2
Output: [[2, 2, 2], [2, 1, 2], [2, 2, 2]]

 

Note:

  1. 1 <= grid.length <= 50
  2. 1 <= grid[0].length <= 50
  3. 1 <= grid[i][j] <= 1000
  4. 0 <= r0 < grid.length
  5. 0 <= c0 < grid[0].length
  6. 1 <= color <= 1000

解题思路:题目本身不难,但是要理解清楚题目的意思。怎么判断一个方格是否是 The border of a connected component?有两个条件,一是这个方格处在grid的边界上,二是这个方格的上下左右四个方向的方格的颜色和自身不都一样。我的方法是分两步,第一步把和输入方格connect的所有方格都找出来,第二部就是依次判断这些connect的方格是否处于border,如果处于则修改颜色。

代码如下:

class Solution(object):
    def colorBorder(self, grid, r0, c0, color):
        """
        :type grid: List[List[int]]
        :type r0: int
        :type c0: int
        :type color: int
        :rtype: List[List[int]]
        """
        visit = []
        for i in grid:
            visit.append([0] * len(i))
        queue = [(r0,c0)]
        visit[r0][c0] = 1
        val = grid[r0][c0]
        direction = [(1,0),(-1,0),(0,1),(0,-1)]
        while len(queue) > 0:
            x,y = queue.pop(0)
            #grid[x][y] = color
            for dx,dy in direction:
                if x + dx >= 0 and x + dx < len(grid) and y +dy >=0 and y+dy < len(grid[0]) \
                    and visit[x+dx][y+dy] == 0 and val == grid[x+dx][y+dy]:
                    visit[x + dx][y + dy] = 1
                    queue.append((x+dx,y+dy))

        for i in range(len(visit)):
            for j in range(len(visit[i])):
                if i == 1 and j == 1:
                    pass
                if visit[i][j] == 0:
                    continue
                elif i == 0 or i == len(visit) - 1 or j == 0 or j == len(visit[i]) - 1:
                    grid[i][j] = color
                else:
                    for dx, dy in direction:
                        if i + dx >= 0 and i + dx < len(grid) and j + dy >= 0 and j + dy < len(grid[0]) \
                                and visit[i + dx][j + dy] == 0 :
                            grid[i][j] = color
                            break
        #print visit
        return grid

 

posted @ 2019-05-24 15:11  seyjs  阅读(404)  评论(0编辑  收藏  举报