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.
classCell:def__init__(self,x,y,value,board):self.x=xself.y=yself.value=valueself.board=boardself.visited=Falsedef__str__(self):returnstr(self.x)+"-"+str(self.y)+":"+str(self.value)defisEmpty(self):returnself.value==0defsetVisited(self,value):self.visited=valuedefisVisited(self):returnself.visiteddefconnectedSibling(self):if(self.isEmpty()):return{}else:siblings={}foriinrange(-1,2):forjinrange(-1,2):ifi==0andj==0:continuenextSibling=self.board.getCell(self.x+i,self.y+j)ifnextSibling!=None:ifnotnextSibling.isEmpty()andnotnextSibling.isVisited():nextSibling.setVisited(True)siblings[str(self.x+i)+"_"+str(self.y+j)]=nextSibling#Verifico se ha celle adiacenti:sibOfSib=nextSibling.connectedSibling()forsOfsinsibOfSib.keys():siblings[sOfs]=sibOfSib[sOfs]siblings[str(self.x)+"_"+str(self.y)]=selfreturnsiblingsclassBoard:cells={}def__init__(self,matrix):forrowinrange(len(matrix)):forcolinrange(len(matrix[row])):self.cells[str(row)+"_"+str(col)]=Cell(row,col,matrix[row][col],self)defgetCell(self,x,y):try:returnself.cells[str(x)+"_"+str(y)]exceptKeyError:returnNonedefgetCells(self):returnself.cellsdefconnectedCell(matrix):# Write your code here#print ( matrix )board=Board(matrix)cells=board.getCells()maxConnected=0forcincells.values():connected=c.connectedSibling()ifmaxConnected<len(connected):maxConnected=len(connected)returnmaxConnected
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 using OO approach