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/python3importmathimportosimportrandomimportreimportsysimportcopy## Complete the 'twoPluses' function below.## The function is expected to return an INTEGER.# The function accepts STRING_ARRAY grid as parameter.#classValidPlus:def__init__(self):self.area=1self.length=1self.cells=[]defaddCell(self,cell):self.cells.append(cell)classCell:def__init__(self,row,column):self.row=rowself.column=columndef__str__(self):returnstr(self.row)+str(self.column)def__hash__(self):returnhash(str(self))def__eq__(self,other):returnself.row==other.rowandself.column==other.columndef__gt__(self,other):returnself.row>other.rowandself.column>other.columndef__lt__(self,other):returnself.row<other.rowandself.column<other.columnif__name__=='__main__':n,m=input().strip().split(' ')n,m=[int(n),int(m)]grid=[]grid_i=0forgrid_iinrange(n):grid_t=str(input().strip())grid.append(grid_t)validPluses=[]foriinrange(n):forjinrange(m):current_cell=grid[i][j]ifcurrent_cell=='G':there_is_a_good_cell=TruevalidPlus=ValidPlus()length=1validPlus.addCell(Cell(i,j))validPluses.append(validPlus)whilethere_is_a_good_cell:if(i-length>=0andgrid[i-length][j]=='G')and(i+length<nandgrid[i+length][j]=='G')and(j-length>=0andgrid[i][j-length]=='G')and(j+length<mandgrid[i][j+length]=='G'):new_valid_plus=copy.deepcopy(validPlus)new_valid_plus.addCell(Cell(i-length,j))new_valid_plus.addCell(Cell(i+length,j))new_valid_plus.addCell(Cell(i,j-length))new_valid_plus.addCell(Cell(i,j+length))length+=1new_valid_plus.area=1+(4*(length-1))new_valid_plus.length=lengthvalidPluses.append(new_valid_plus)validPlus=copy.deepcopy(new_valid_plus)else:there_is_a_good_cell=Falsemax_area=0foriinrange(len(validPluses)):current_valid_plus=validPluses[i]forjinrange(i+1,len(validPluses)):other_valid_plus=validPluses[j]current_area=current_valid_plus.area*other_valid_plus.areaifcurrent_area>max_area:current_cells=current_valid_plus.cellsother_cells=other_valid_plus.cellscommon_cells=set(current_cells)-(set(current_cells)-set(other_cells))iflen(common_cells)==0:max_area=current_areaprint(max_area)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ema's Supercomputer
You are viewing a single comment's thread. Return to all comments →
Ema's Supercomputer solution in Python