

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find number of islands, from where we cannot leave in Python
Suppose we have a binary matrix. Here 1 represents land and 0 represents water. From any land we can move up, down, left or right but not diagonally to another land cell or go off the matrix. We have to find the number of land cells from which we cannot go off the matrix.
So, if the input is like
0 | 0 | 0 | 1 |
0 | 1 | 1 | 0 |
0 | 1 | 1 | 0 |
0 | 0 | 0 | 1 |
then the output will be 4, as There's 4 land squares in the middle from which we cannot walk off the matrix.
To solve this, we will follow these steps −
- q := a list of pairs (i, j) for each row i and column when matrix[i, j] is land i and j are border indices
- idx := 0
- for each pair (x, y) in q, do
- matrix[x, y] := 0
- while idx < size of q, do
- x, y := q[idx]
- for each (dx, dy) in [(-1, 0) ,(0, -1) ,(0, 1) ,(1, 0) ], do
- nx := x + dx
- ny := y + dy
- if 0 <= nx < row count of matrix and 0 <= ny < column count of matrix[nx] and matrix[nx, ny] is 1, then
- matrix[nx, ny] := 0
- insert (nx, ny) at the end of q
- idx := idx + 1
- return sum of all elements of matrix
Example
Let us see the following implementation to get better understanding −
def solve(matrix): q = [(i, j) for i in range(len(matrix)) for j in range(len(matrix[i])) if matrix[i][j] and (i == 0 or i == len(matrix) - 1 or j == 0 or j == len(matrix[i]) - 1)] idx = 0 for x, y in q: matrix[x][y] = 0 while idx < len(q): x, y = q[idx] for dx, dy in [(-1, 0), (0, -1), (0, 1), (1, 0)]: nx, ny = x + dx, y + dy if 0 <= nx < len(matrix) and 0 <= ny < len(matrix[nx]) and matrix[nx][ny]: matrix[nx][ny] = 0 q.append((nx, ny)) idx += 1 return sum(sum(row) for row in matrix) matrix = [ [0, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1] ] print(solve(matrix))
Input
[ [0, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1] ]
Output
4
- Related Questions & Answers
- Program to find number of starting point from where we can start travelling in Python
- Number of Islands in Python
- Program to find distance of shortest bridge between islands in Python
- Program to count number of islands in a given matrix in Python
- Program to count number of surrounded islands in the matrix in python
- Program to count number of overlapping islands in two maps in Python
- Find the number of distinct islands in a 2D matrix in Python
- Find the number of islands Using DFS in C++
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Find the number of Islands Using Disjoint Set in C++
- Number of Closed Islands in C++
- Number of Distinct Islands in C++
- Program to find nearest number of n where all digits are odd in python
- Program to find maximum number of coins we can collect in Python
- Program to find number of ways where square of number is equal to product of two numbers in Python
Advertisements