You are viewing a single comment's thread. Return to all comments →
Ruby:
def cover_neighbours(i,j,m,n) return if i>=n || j>=m || i<0 || j<0 return if @matrix[i][j] == 0 @matrix[i][j] = 0 @count += 1 cover_neighbours(i+1,j,m,n) cover_neighbours(i-1,j,m,n) cover_neighbours(i,j+1,m,n) cover_neighbours(i,j-1,m,n) cover_neighbours(i+1,j-1,m,n) cover_neighbours(i-1,j+1,m,n) cover_neighbours(i+1,j+1,m,n) cover_neighbours(i-1,j-1,m,n) end def connectedCell(matrix) n = matrix.size m = matrix[0].size @matrix = matrix max_count = 0 for i in 0..(n-1) do for j in 0..(m-1) do if @matrix[i][j] == 1 @count = 0 cover_neighbours(i,j,m,n) max_count = @count if @count > max_count end end end return max_count end
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 →
Ruby: