You are viewing a single comment's thread. Return to all comments →
def connectedCell(matrix) visited = [] max_counter = 0 matrix.length.times do |i| matrix[0].length.times do |j| if matrix[i][j] == 1 and not visited.include?([i,j]) queue = [[i,j]] visited << [i,j] counter = 1 while queue.length > 0 m,n = queue.shift valid_states = [ [m+1,n+1], [m+1,n], [m+1,n-1], [m-1,n+1], [m-1,n], [m-1,n-1], [m, n+1],[m,n-1]] valid_states = valid_states.select{ |m,n| m>=0 and m < matrix.length and n>=0 and n < matrix[0].length } valid_states = valid_states.select{ |m,n| matrix[m][n] == 1 and not visited.include?([m,n]) } visited += valid_states queue += valid_states counter += valid_states.length end max_counter = counter if counter > max_counter end end end max_counter 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