Algorithm 以上两个例子的计算结果。谢谢。第一个例子:大小=2+3-2-1=4。第二个例子:3+3-1-1+1-1=6。neighbor_blocks是连接的“块”顶点数。 [0,0,0,0] [0,1,1,0] [1,0,1,0] def get_all_coo

Algorithm 以上两个例子的计算结果。谢谢。第一个例子:大小=2+3-2-1=4。第二个例子:3+3-1-1+1-1=6。neighbor_blocks是连接的“块”顶点数。 [0,0,0,0] [0,1,1,0] [1,0,1,0] def get_all_coo,algorithm,graph,grid,breadth-first-search,microsoft-distributed-file-system,Algorithm,Graph,Grid,Breadth First Search,Microsoft Distributed File System,以上两个例子的计算结果。谢谢。第一个例子:大小=2+3-2-1=4。第二个例子:3+3-1-1+1-1=6。neighbor_blocks是连接的“块”顶点数。 [0,0,0,0] [0,1,1,0] [1,0,1,0] def get_all_coordinates_of_ones(grid): set_ones = set() for i in range(len(grid[0])): for j in range(len(grid)):


以上两个例子的计算结果。谢谢。第一个例子:大小=2+3-2-1=4。第二个例子:3+3-1-1+1-1=6。neighbor_blocks是连接的“块”顶点数。
[0,0,0,0]
[0,1,1,0]
[1,0,1,0] 
def get_all_coordinates_of_ones(grid):
    set_ones = set()
    for i in range(len(grid[0])):
        for j in range(len(grid)):
            if grid[i][j]:
               set_ones.add((i, j))

    return set_ones

def get_largest_region(x, y, grid):

    num_col = len(grid)
    num_row = len(grid[0])
    one_or_zero = grid[x][y]

    if not grid[x][y]:
       grid[x][y] = 1 - grid[x][y]

    # get the coordinates of ones in the grid
    # Worst Case O(num_col * num_row)
    coordinates_ones = get_all_coordinates_of_ones(grid)

    while coordinates_ones:
       queue = collections.deque([coordinates_ones.pop()])
       largest_one = float('-inf')
       count_one = 1
       visited = set()
       while queue:
           x, y = queue.popleft()
           visited.add((x, y))
           for new_x, new_y in ((x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y)):
               if (0 <= new_x < num_row and 0 <= new_y < num_col):
                   if grid[new_x][new_y] == 1 and (new_x, new_y) not in visited:
                       count_one += 1
                       if (new_x, new_y) in coordinates_ones:-
                           coordinates_ones.remove((new_x, new_y))
                       queue.append((new_x, new_y))
       largest_one = max(largest_one, count_one)
    return largest_one