leetcode 200.岛屿数量(中等,深度优先搜索+回溯)

岛屿数量:
在这里插入图片描述

这一题与单词搜索一题思路相同;

用深度优先搜索来解决本题;

对于此类问题,需要两步,

第一步需要需要遍历网格的每个位置,设置一个访问数组,防止元素被重复访问;

第二步是对于每个访问到的位置,需要向四个方向进行寻找,因此向四个方向进行搜索;

递归终止条件:

如果访问到字符’0’,就行返回,(回溯)。

代码如下:

class Solution:
    def numIslands(self, grid):

        #岛屿数量
        numsiland = 0

        #设置一个used数组,看是否被访问过;
        used = [[False] * len(grid[0]) for i in range(len(grid))]
        # print(used)

        for i in range(len(grid)):
            for j in range(len(grid[0])):

                if used[i][j] == False and grid[i][j] == '1':
                    
                    numsiland += 1

                    self.dfs_search(i, j, used, grid, numsiland)
                    


        return numsiland



    def dfs_search(self, x, y, used, grid,numsiland):

        #递归终止条件
        if grid[x][y] == '0' and used[x][y] == True:
            return


        # print(numsiland)
        

        # print(x)


        #四个方向进行遍历
        #向右遍历,x表示行,y表示列
        if y < len(grid[0]) - 1:
            # print(x + 1)
            # print(y)
            if used[x][y + 1] == False:

                used[x][y + 1] = True

                self.dfs_search(x, y + 1, used, grid,numsiland)

        #向下遍历
        if x < len(grid) - 1:
            if used[x + 1][y] == False:

                used[x + 1][y] = True
                self.dfs_search(x + 1, y, used, grid,numsiland)
        
        #向左遍历
        if y > 0:
            if used[x][y - 1] == False:

                used[x][y - 1] = True


                self.dfs_search(x, y - 1, used, grid, numsiland)

        #向上遍历
        if x > 0:
            if used[x - 1][y] == False:

                used[x - 1][y] = True

                self.dfs_search(x - 1, y, used, grid, numsiland)
        

代码优化:

优化掉设置的访问数组used;可以通过设置访问过的元素设置为字符‘2’,来去掉访问数组的设置;

代码如下:

class Solution:
    def numIslands(self, grid):

        #岛屿数量
        numsiland = 0

        #设置一个used数组,看是否被访问过;
        # used = [[False] * len(grid[0]) for i in range(len(grid))]
        # print(used)

        for i in range(len(grid)):
            for j in range(len(grid[0])):

                if used[i][j] == False and grid[i][j] == '1':
                    
                    numsiland += 1

                    self.dfs_search(i, j, grid, numsiland)
                    


        return numsiland



    def dfs_search(self, x, y, used, grid,numsiland):

        #递归终止条件
        if grid[x][y] != '1':
            return


        # print(numsiland)
        

        # print(x)
        gird[i][j] = '2'


        #四个方向进行遍历
        #向右遍历,x表示行,y表示列
        if y < len(grid[0]) - 1:
            # print(x + 1)
            # print(y)
            # if used[x][y + 1] == False:

            # used[x][y + 1] = True

            self.dfs_search(x, y + 1, grid,numsiland)

        #向下遍历
        if x < len(grid) - 1:
            # if used[x + 1][y] == False:

                # used[x + 1][y] = True
            self.dfs_search(x + 1, y, grid,numsiland)
        
        #向左遍历
        if y > 0 :
            # if used[x][y - 1] == False:

                # used[x][y - 1] = True


            self.dfs_search(x, y - 1, grid, numsiland)

        #向上遍历
        if x > 0:
            # if used[x - 1][y] == False:

                # used[x - 1][y] = True

            self.dfs_search(x - 1, y, grid, numsiland)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值