Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 14, 2018 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/2ac7d8a6dfb34294856b17c07b03a80d to your computer and use it in GitHub Desktop.
Save jianminchen/2ac7d8a6dfb34294856b17c07b03a80d to your computer and use it in GitHub Desktop.
Island count recursive January 14, 2018 - need to check compile error
using System;
class Solution
{
static int getNumberOfIslands(int[,] binaryMatrix) { //
if(binaryMatrix == null || binaryMatrix.GetLength(0) == 0 || binaryMatrix.GetLength (1) == 0) // false
{
return 0;
}
int rows = binaryMatrix.GetLength(0) ; // 5
int cols = binaryMatrix.GetLength(1); // 5
int islandFound = 0;
for(int row = 0; row < rows; row++)
{
for(int col = 0; col < cols; col++)
{
var current = binaryMatrix[row, col]; // 0 , -1
if(current != 1)
{
continue;
}
visitIsland(binaryMatrix, row, col);
islandFound++;
}
}
}
private void visitIsland(int[,] binaryMatrix, int startRow, int startCol) // 0, 1
{
int rows = binaryMatrix.GetLength(0) ; // 5
int cols = binaryMatrix.GetLength(1); // 5
if(binaryMatrix[startRow, startCol] != 1)
{
return;
}
// mark visit
binaryMatrix[startRow, startCol] = -1; // -1
// clockwise 4 neigbhors
var leftCol = startCol - 1;
if(leftCol >= 0) // false
{
visitIsland(binaryMatrix, startRow, leftCol);
}
// right
var rightCol = startCol + 1;
if(startCol < (cols - 1)) // false
{
visitIsland(binaryMatrix, startRow, rightCol);
}
// top
var topRow = startRow - 1;
if(startRow > 0 ) // false
{
visitIsland(binaryMatrix, topRow, startCol);
}
// down
var downRow = startRow + 1;
if(startRow < rows - 1)//false
{
visitIsland(binaryMatrix, downRow, startCol);
}
}
public static void main(String[] args) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment