debugging puts @visited.inspect不显示任何内容

4urapxun  于 2023-05-13  发布在  其他
关注(0)|答案(1)|浏览(121)

我试图理解这段代码,所以我放了一个puts @visited.inspect或一个puts visited.inspect,但什么都没有显示。代码运行正常,如下所示

grid = [  ["1","1","1","1","0"],  ["1","1","0","1","0"],  ["1","1","0","0","0"],  ["0","0","0","0","0"]]

def max_area_of_island(grid)
    @visited = Set.new
    @grid = grid

    total_sum = [0] 
    @m = grid.length 
    @n = grid[0].length 

    i = 0
    while i < @m 
        j = 0 
        while j < @n
            if grid[i][j] == 1 && !@visited.include?([i,j])
               @visited << [i,j]
                puts @visited.inspect
               total_sum << bfs([i,j])
            end 
            j += 1
        end 
        i += 1
    end 

    total_sum.max
end

我把一个puts @visited.inspect和一个puts visited.inspect
代码运行顺利,但它没有打印任何东西。我能做些什么来打印@visited的值,只是为了检查程序正在做什么或...你建议我应该调试这段代码吗?在哪?哪个项目?

bfrts1fy

bfrts1fy1#

您的grid变量存储字符串,但在下面,您将grid[i][j]与整数进行比较。这将永远是假的。

if grid[i][j] == 1 && !@visited.include?([i,j])

没有bfs的定义,我无法验证这是否是您的代码的唯一问题。

相关问题