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.
funcrSearch(matrix*[][]int32,r,c,rows,colsint32)int32{sum:=int32(1)(*matrix)[r][c]=int32(2)// mark cell as visitedfori:=int32(-1);i<=1;i++{forj:=int32(-1);j<=1;j++{if(i==0&&j==0)||r+i<0||r+i>=rows||c+j<0||c+j>=cols{continue}if(*matrix)[r+i][c+j]==1{sum+=rSearch(matrix,r+i,c+j,rows,cols)}}}returnsum}funcconnectedCell(matrix[][]int32)int32{// - iterate over matrix left to right top to bottom// - when finding a cell, recursively explore the unvisited neighborsrows:=int32(len(matrix))cols:=int32(len(matrix[0]))varmSizeint32forr:=int32(0);r<rows;r++{forc:=int32(0);c<cols;c++{ifmatrix[r][c]==1{size:=rSearch(&matrix,r,c,rows,cols)ifsize>mSize{mSize=size}}}}returnmSize}
Connected Cells in a Grid
You are viewing a single comment's thread. Return to all comments →
Here's a simple solution Go: