You are viewing a single comment's thread. Return to all comments →
def connectedCell(matrix): row,col = len(matrix),len(matrix[0]) visited = [[0]*col for _ in range(row)] ans = 0 def dfsCount(data,visited,i,j,row,col): if i<0 or i>=row or j<0 or j>=col or data[i][j]!=1 or visited[i][j]!=0: return 0 visited[i][j]=1 return dfsCount(data,visited,i-1,j,row,col)+dfsCount(data,visited,i+1,j,row,col)+dfsCount(data,visited,i,j-1,row,col)+dfsCount(data,visited,i,j+1,row,col)+dfsCount(data,visited,i-1,j-1,row,col)+dfsCount(data,visited,i+1,j+1,row,col)+dfsCount(data,visited,i-1,j+1,row,col)+dfsCount(data,visited,i+1,j-1,row,col)+1 for i in range(row): for j in range(col): if visited[i][j]==0 and matrix[i][j]==1: count = dfsCount(matrix,visited,i,j,row,col) ans = max(count,ans) return ans
Seems like cookies are disabled on this browser, please enable them to open this website
Connected Cells in a Grid
You are viewing a single comment's thread. Return to all comments →