使用DFS的岛屿数量

发布于 2025-01-26 23:00:55 字数 1441 浏览 3 评论 0原文

帮助我发现错误

答案错误 细节 输入 实际“ 0”,“ 1”,“ 0”,“ 0”],[“ 0”,“ 0”,“ 0”,“ 1”,“ 1”]]] 输出 1 预期的 3

使用深度第一遍历来计数岛屿

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        #Nested loop to go over our Grid
        #use depth first traversal in second loop to explore neighbors
        
        visited = set()
        count = 0
        
        for row in range(len(grid)):
            for col in range(len(grid[0])):
                if (self.explore(grid,row,col,visited)):
                    count +=1 
        return count
                
    def explore(self,grid,row,col,visited):
        #catch if we are out of bounds
        rowInbound = 0 <= row and row < len(grid)
        colInbound = 0 <= col and col < len(grid[0])
        
        if not rowInbound or not colInbound:
            return False
        
        #check if current is water
        position = grid[row][col]
        if (position == '0'):
            return False
        
        #check for visited 
        if position in visited:
            return False
        
        visited.add(position)
        
        self.explore(grid,row-1,col,visited)
        self.explore(grid,row+1,col,visited)
        self.explore(grid,row,col-1,visited)
        self.explore(grid,row,col+1,visited)
        
        #this true will symbolize that I am now exploring a new island
        
        return True     

Help me spot the error

Wrong Answer
Details
Input
[["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
Output
1
Expected
3

Using a depth first traversal to count islands

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        #Nested loop to go over our Grid
        #use depth first traversal in second loop to explore neighbors
        
        visited = set()
        count = 0
        
        for row in range(len(grid)):
            for col in range(len(grid[0])):
                if (self.explore(grid,row,col,visited)):
                    count +=1 
        return count
                
    def explore(self,grid,row,col,visited):
        #catch if we are out of bounds
        rowInbound = 0 <= row and row < len(grid)
        colInbound = 0 <= col and col < len(grid[0])
        
        if not rowInbound or not colInbound:
            return False
        
        #check if current is water
        position = grid[row][col]
        if (position == '0'):
            return False
        
        #check for visited 
        if position in visited:
            return False
        
        visited.add(position)
        
        self.explore(grid,row-1,col,visited)
        self.explore(grid,row+1,col,visited)
        self.explore(grid,row,col-1,visited)
        self.explore(grid,row,col+1,visited)
        
        #this true will symbolize that I am now exploring a new island
        
        return True     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生一个溪 2025-02-02 23:00:55

问题是位置访问无关。 位置是单元格的 content ,而访问应收集单元格的坐标

因此,将相关部分更改为:

    if (row, col) in visited:
        return False
    
    visited.add((row, col))

The problem is that position has nothing to do with visited. position is the content of a cell, while visited should collect coordinates of a cell.

So change the relevant part to:

    if (row, col) in visited:
        return False
    
    visited.add((row, col))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文