0% found this document useful (0 votes)
5 views2 pages

Practice 1

The task requires 1st year students to solve the 'Number of Islands' problem using BFS and DFS algorithms on a 2D grid of '1's (land) and '0's (water). Students must implement two functions, numIslandsBFS() and numIslandsDFS(), to count the number of islands by exploring connected land cells while marking visited cells. The submission should include clean, well-commented code and sample test cases to verify the solution.

Uploaded by

sheri.dosik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Practice 1

The task requires 1st year students to solve the 'Number of Islands' problem using BFS and DFS algorithms on a 2D grid of '1's (land) and '0's (water). Students must implement two functions, numIslandsBFS() and numIslandsDFS(), to count the number of islands by exploring connected land cells while marking visited cells. The submission should include clean, well-commented code and sample test cases to verify the solution.

Uploaded by

sheri.dosik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Task for 1st Year Students: Solve

"Number of Islands" Using BFS and DFS

💡 Task Description:
You are given a 2D grid map representing a map of '1's (land) and '0's (water). Your goal is
to find the number of islands in the grid. An island is surrounded by water and is formed by
connecting adjacent lands horizontally or vertically. You can assume that all four edges of
the grid are surrounded by water.

🧩 Problem Statement:
● The grid is represented as a 2D array of characters, where:
○ '1' represents land.
○ '0' represents water.
● An island is a group of connected '1's (land) cells connected horizontally or
vertically.
● You need to count how many islands are present in the grid.

grid = [
["1", "1", "0", "0", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"]
]
Expected result
3

You need to solve this problem using both BFS (Breadth-First Search) and DFS
(Depth-First Search) algorithms.

Steps to Follow:

1. Implement the function using BFS:


○ Use a queue to explore neighboring cells.
○ Mark visited cells to avoid counting them multiple times.
2. Implement the function using DFS:
○ Use recursion or a stack to explore neighboring cells.
○ Mark visited cells to avoid counting them multiple times.
int numIslandsBFS(vector<vector<char>>& grid);
int numIslandsDFS(vector<vector<char>>& grid);

Task Requirements:

1. Write two separate functions to solve the problem:


○ numIslandsBFS(): Using Breadth-First Search.
○ numIslandsDFS(): Using Depth-First Search.
2. Input and Output:
○ Input: 2D grid of characters ('1' or '0').
○ Output: Integer representing the number of islands.
3. Code Quality:
○ Write clean and well-commented code.
○ Explain your logic in comments.
○ Use meaningful variable names.

📝 Submission:
1. Submit your solution as a single file.
2. The file should include both BFS and DFS implementations.
3. Add sample test cases to verify your solution.

You might also like