Created
April 20, 2017 17:07
-
-
Save jianminchen/50fdaeaa1f7b2d6b58c6e00616f1ceb2 to your computer and use it in GitHub Desktop.
Flood fill - count island - code review after mocking experience, fix grammar errors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class LearningExperience { | |
static void Main(string[] args) { | |
Console.WriteLine("Meet a new person and talk about flood fill algorithm"); | |
} | |
public static int CountIsland(int[][] matrix) | |
{ | |
if(matrix == null || matrix.Length == 0 || matrix[0].Length==0) return 0; | |
int rows = matrix.Length; | |
int cols = matrix[0].Length; | |
int islandCount = 0; | |
for(int row = 0 ; row < rows; row++) | |
{ | |
for(int col = 0; col < cols; col++) | |
{ | |
var current = matrix[row][col]; | |
if(current != 1) continue; | |
visitIslandBFS(matrix, row, col); | |
islandCount ++; | |
} | |
} | |
return islandCount; | |
} | |
private static void visitIslandBFS(int[][] matrix, int row, int col) | |
{ | |
Queue<string> queue = new Queue<string>(); | |
var key = encodeKey(row,col); | |
queue.Enqueue(key); | |
while(queue.Count > 0) | |
{ | |
var current = queue.Dequeue(); | |
var positions = Array.ConvertAll(current.Split('+'), int.Parse); | |
int visit_row = positions[0]; | |
int visit_col = positions[1]; | |
matrix[visit_row][visit_col] = 2; | |
// left | |
int left_row = visit_row; | |
int left_col = visit_col-1; | |
if(isInBoundary(matrix, left_row, left_col) && matrix[left_row][left_col] == 1) | |
{ | |
var neighborKey = encodeKey( left_row,left_col); | |
queue.Enqueue(neighborKey); | |
} | |
//right | |
int right_row = visit_row; | |
int right_col = visit_col + 1; | |
if(isInBoundary(matrix, right_row, right_col) && matrix[right_row][right_col] == 1) | |
{ | |
var neighborKey = encodeKey( right_row,right_col); | |
queue.Enqueue(neighborKey); | |
} | |
// top | |
int top_row = visit_row - 1; | |
int top_col = visit_col ; | |
if(isInBoundary(matrix, top_row, top_col) && matrix[top_row][top_col] == 1) | |
{ | |
var neighborKey = encodeKey( top_row,top_col); | |
queue.Enqueue(neighborKey); | |
} | |
// bottom | |
int bottom_row = visit_row + 1; | |
int bottom_col = visit_col; | |
if(isInBoundary(matrix, bottom_row, bottom_col) && matrix[bottom_row][bottom_col] == 1) | |
{ | |
var neighborKey = encodeKey( bottom_row,bottom_col); | |
queue.Enqueue(neighborKey); | |
} | |
} | |
} | |
private static string encodeKey(int row, int col) | |
{ | |
return row +"+"+ col; | |
} | |
private static bool isInBoundary(int[][] matrix, int row, int col) | |
{ | |
int rows = matrix.Length; | |
int cols = matrix[0].Length; | |
return (row >= 0 && row < rows && col >=0 && col < cols); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment