
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to print number of islands in a given matrix using C#?
Linear scan the 2d grid map, if a node contains a '1', then it is a root node that triggers a Depth First Search. During DFS, every visited node should be set as '0' to mark as visited node. Count the number of root nodes that trigger DFS, this number would be the number of islands since each DFS starting at some root identifies an island.
Example
using System; namespace ConsoleApplication{ public class Matrix{ public int PrintNumberOfIslands(char[,] grid){ bool[,] visited = new bool[grid.GetLength(0), grid.GetLength(1)]; int res = 0; for (int i = 0; i < grid.GetLength(0); i++){ for (int j = 0; j < grid.GetLength(1); j++){ if (grid[i, j] == '1' && !visited[i, j]){ DFS(grid, visited, i, j); res++; } } } return res; } public void DFS(char[,] grid, bool[,] visited, int i, int j){ if (i < 0 || i >= grid.GetLength(0)) return; if (j < 0 || j >= grid.GetLength(1)) return; if (grid[i, j] != '1' || visited[i, j]) return; visited[i, j] = true; DFS(grid, visited, i + 1, j); DFS(grid, visited, i - 1, j); DFS(grid, visited, i, j + 1); DFS(grid, visited, i, j - 1); } } class Program{ static void Main(string[] args){ Matrix m = new Matrix(); char[,] mm = { { '1', '1', '1', '1', '0' }, { '1', '1', '0', '1', '0' }, { '1', '1', '0', '0', '0' }, { '0', '0', '0', '0', '1' } }; Console.WriteLine(m.PrintNumberOfIslands(mm)); } } }
Output
2
- Related Articles
- Program to count number of islands in a given matrix in Python
- Find the number of distinct islands in a 2D matrix in Python
- Program to count number of surrounded islands in the matrix in python
- How to print maximum number of A’s using given four keys
- Find the number of islands Using DFS in C++
- Number of Islands in Python
- Find the number of Islands Using Disjoint Set in C++
- Java program to print a given matrix in Spiral Form.
- Number of Closed Islands in C++
- Number of Distinct Islands in C++
- Print a given matrix in zigzag form in C++
- How to print a matrix of size n*n in spiral order using C#?
- Finding the number of rows and columns in a given matrix using Numpy
- Print a given matrix in reverse spiral form in C++
- Number of Distinct Islands II in C++

Advertisements