We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'connectedCell' function below.## The function is expected to return an INTEGER.# The function accepts 2D_INTEGER_ARRAY matrix as parameter.#defconnectedCell(matrix):# Write your code hereones=[]foriinrange(len(matrix)):forjinrange(len(matrix[0])):ifmatrix[i][j]==1:ones.append((i,j))l=[]whileones!=[]:one=ones.pop(0)ll=[one]m=1n=0whilen!=m:m=len(ll)foroneinll:i,j=one[0],one[1]neighbors=[(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]forneighborinneighbors:ifneighborinones:ones.remove(neighbor)ll.append(neighbor)n=len(ll)l.append(len(ll))returnmax(l)if__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')n=int(input().strip())m=int(input().strip())matrix=[]for_inrange(n):matrix.append(list(map(int,input().rstrip().split())))result=connectedCell(matrix)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
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 →
Solution in Python3: