Leetcode 学习计划之21天算法 (七)

 第7天 广度优先搜索 / 深度优先搜索

733.图像渲染 

 思路:

1、DFS。用递归实现,设置seen数组,如不设置,在遇到newcolor==oldcolor时会超时,不断重复遍历。 这里也可以在开始做一个判断:如果newcolor==oldcolor直接返回原图,就不用seen数组了。上下左右四个方向。

#经典trick:
for x, y in[(sr+1,sc), (sr-1, sc),(sr,sc+1),(sr,sc-1)]:

2、BFS。用队列实现,设置seen数组。

#DFS+seen
class Solution(object):
    def floodFill(self, image, sr, sc, newColor):
        """
        :type image: List[List[int]]
        :type sr: int
        :type sc: int
        :type newColor: int
        :rtype: List[List[int]]
        """
        l = len(image)
        w = len(image[0])
        def dfs(image, oldcolor, newcolor, sr, sc,seen):
            for x, y in[(sr+1,sc), (sr-1, sc),(sr,sc+1),(sr,sc-1)]:
                if 0 <= x < l and 0 <= y < w and seen[x][y] == 0 and image[x][y] == oldcolor:
                    image[x][y] = newcolor
                    seen [x][y] = 1
                    dfs(image, oldcolor, newcolor, x, y, seen)
                    
        seen = [[0 for i in range(w)] for i in range(l)]
        seen[sr][sc] = 1
        t = image[sr][sc]
        image[sr][sc] = newColor
        dfs(image, t, newColor, sr, sc, seen)         
        return image
#dfs+old new判断
class Solution(object):
    def floodFill(self, image, sr, sc, newColor):
        """
        :type image: List[List[int]]
        :type sr: int
        :type sc: int
        :type newColor: int
        :rtype: List[List[int]]
        """
        l = len(image)
        w = len(image[0])
        def dfs(image, oldcolor, newcolor, sr, sc):
            for x, y in[(sr+1,sc), (sr-1, sc),(sr,sc+1),(sr,sc-1)]:
                if 0 <= x < l and 0 <= y < w and image[x][y] == oldcolor:
                    image[x][y] = newcolor
                    dfs(image, oldcolor, newcolor, x, y)
                    
        t = image[sr][sc]
        image[sr][sc] = newColor
        if t == newColor:
            return image
        dfs(image, t, newColor, sr, sc)         
        return image
#BFS+seen数组
class Solution(object):
    def floodFill(self, image, sr, sc, newColor):
        """
        :type image: List[List[int]]
        :type sr: int
        :type sc: int
        :type newColor: int
        :rtype: List[List[int]]
        """
        l = len(image)
        w = len(image[0])
        queue = []
        def bfs(image, oldcolor, newcolor, r, c,seen):
            image[r][c] = newcolor
            seen [r][c] = 1
            queue.append([r,c])  #入队
            while queue:         #只要队不空
                sr, sc = queue.pop() #出队
                for x, y in[(sr+1,sc), (sr-1, sc),(sr,sc+1),(sr,sc-1)]: #处理出队结点的邻接结点
                    if 0 <= x < l and 0 <= y < w and seen[x][y] == 0 and image[x][y] == oldcolor:
                        image[x][y] = newcolor
                        seen [x][y] = 1
                        queue.append([x,y])
                    
        seen = [[0 for i in range(w)] for i in range(l)]
        t = image[sr][sc]
        bfs(image, t, newColor, sr, sc, seen)         
        return image
#Bfs+old new判断
class Solution(object):
    def floodFill(self, image, sr, sc, newColor):
        """
        :type image: List[List[int]]
        :type sr: int
        :type sc: int
        :type newColor: int
        :rtype: List[List[int]]
        """
        l = len(image)
        w = len(image[0])
        queue = []
        def bfs(image, oldcolor, newcolor, r, c):
            image[r][c] = newcolor
            queue.append([r,c])
            while queue:
                sr, sc = queue.pop()
                for x, y in[(sr+1,sc), (sr-1, sc),(sr,sc+1),(sr,sc-1)]:
                    if 0 <= x < l and 0 <= y < w and image[x][y] == oldcolor:
                        image[x][y] = newcolor
                        queue.append([x,y])
                    
        t = image[sr][sc]
        if t == newColor:
            return image
        bfs(image, t, newColor, sr, sc)         
        return image

695.岛屿的最大面积

1、BFS 上下左右四个方向。

2、DFS:只需把BFS的queue改为stack即可,即把pop(0)改为pop()

#BFS
class Solution(object):
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        def bfs(grid, ii, jj, seen):
            queue = []
            queue.append([ii,jj])
            seen[ii][jj] = 1
            ans = 0
            ans += 1
            while queue:
                i,j = queue.pop(0)
                for x,y in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:
                    if 0<=x<l and 0<=y<w and not seen[x][y] and grid[x][y]:
                        ans += 1
                        seen[x][y] = 1
                        queue.append([x,y])
            return ans
        
        l = len(grid)
        w = len(grid[0])
        max = 0
        seen = [[0 for i in range(w)] for j in range (l)]
        for i in range(l):
            for j in range(w):
                if not seen[i][j] and grid[i][j]:
                    seen[i][j] = 1
                    ans = bfs(grid, i, j, seen)
                    max = ans if max < ans else max
        return max
#DFS
class Solution(object):
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        def dfs(grid, ii, jj, seen):
            queue = []
            queue.append([ii,jj])
            seen[ii][jj] = 1
            ans = 0
            ans += 1
            while queue:
                i,j = queue.pop()
                for x,y in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:
                    if 0<=x<l and 0<=y<w and not seen[x][y] and grid[x][y]:
                        ans += 1
                        seen[x][y] = 1
                        queue.append([x,y])
            return ans
        
        l = len(grid)
        w = len(grid[0])
        max = 0
        seen = [[0 for i in range(w)] for j in range (l)]
        for i in range(l):
            for j in range(w):
                if not seen[i][j] and grid[i][j]:
                    seen[i][j] = 1
                    ans = dfs(grid, i, j, seen)
                    max = ans if max < ans else max
        return max

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值