简体   繁体   English

用于查找岛蟒的 DFS

[英]DFS for finding Island Python

I have been trying this approach for a little while and keep getting 0 for the count of islands.我一直在尝试这种方法一段时间,并不断获得 0 的岛屿数。 I am not quite sure what I am doing wrong here.我不太确定我在这里做错了什么。 As I have started learning python more and more so I may be missing something that is easy to spot.随着我越来越多地开始学习 python,所以我可能会遗漏一些很容易发现的东西。

So I want to find all the islands with a 1 in them and if they are connected (adjacent cells).所以我想找到所有带有 1 的岛屿,以及它们是否相连(相邻单元格)。 So I want to return the count of the number of islands that are found.所以我想返回找到的岛屿数量的计数。

Here is what I have so far.这是我到目前为止所拥有的。

def valid_direction(A, r, c):
    row = len(A)
    col = len(A[0])
    if r < 0 or c < 0 or r >= row or c >= col:
        return False
    else:
        return True

def dfs(A, r, c):
    A[r][c] = '1'
    # Up down left and right
    directions = [(0,1), 
                  (0,-1), 
                  (-1,0),
                  (1,0)] 
    for d in directions:
        nr = r +d[0]
        nc = c + d[1]
        if valid_direction(A, nr, nc) and A[nr][nc] == '1':
            dfs(A, nr, nc)


def solution(A):
    if not A:
        return -1

    row = len(A)
    col = len(A[0])
    results = 0
    for i in range(row):
        for j in range(col):
            if A[i][j] == '1':
                dfs(A, i, j)
                results +=1
    return results

And here are the two arrays I am working with这是我正在使用的两个数组

A1 = [[1,1,1,1,0],
      [1,1,0,1,0],
      [1,1,0,0,0,],
      [0,0,0,0,0]]

A2 = [[1,1,0,0,0],
      [1,1,0,0,0],
      [0,0,1,0,0],
      [0,0,0,1,1]]

You made a simple mistake.你犯了一个简单的错误。 Your arrays are arrays of integers, not chars.您的数组是整数数组,而不是字符数组。
A[r][c] = '1' and A[nr][nc] == '1' and if A[i][j] == '1' A[r][c] = '1'A[nr][nc] == '1'并且if A[i][j] == '1'
should be应该
A[r][c] = 1 and A[nr][nc] == 1 and if A[i][j] == 1 A[r][c] = 1A[nr][nc] == 1if A[i][j] == 1
I'm not sure it will solve the question correctly after this, but this is the current error.我不确定在此之后它会正确解决问题,但这是当前的错误。


Your current code just counts the number of 1s.您当前的代码只计算 1 的数量。
You should check if the 1 is visited ex)by changing it to 2.您应该通过将其更改为 2 来检查 1 是否被访问过。
Also you don't have to initiate directions every time dfs is called you should just take it outside the function.此外,您不必每次调用 dfs 时都启动指示,您应该将其放在函数之外。

directions = [(0,1), 
              (0,-1), 
              (-1,0),
              (1,0)]

def valid_direction(A, r, c):
    row = len(A)
    col = len(A[0])
    if r < 0 or c < 0 or r >= row or c >= col:
        return False
    else:
        return True

def dfs(A, r, c):
    A[r][c] = 2
    # Up down left and right 
    for d in directions:
        nr = r +d[0]
        nc = c + d[1]
        if valid_direction(A, nr, nc) and A[nr][nc] == 1:
            dfs(A, nr, nc)


def solution(A):
    if not A:
        return -1

    row = len(A)
    col = len(A[0])
    results = 0
    for i in range(row):
        for j in range(col):
            if A[i][j] == 1:
                dfs(A, i, j)
                results +=1
    return results

I'm not completely sure what you are trying to solve, but I think the correct code is this.我不完全确定您要解决什么问题,但我认为正确的代码是这样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM