- 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 land with longest distance from water in Python

Suppose we have a binary matrix where, 0 represents water and 1 represents land. Now we have to find the land which has the longest Manhattan distance from water and finally return the distance.

So, if the input is like

1 | 1 | 1 | 1 |

1 | 1 | 0 | 1 |

1 | 1 | 1 | 1 |

0 | 0 | 1 | 1 |

then the output will be 3, as [0,0] cell has Manhattan distance of 3 from water.

To solve this, we will follow these steps −

- if A is empty, then
- return 0

- R := row count of matrix, C := column count of matrix
- distance := a matrix of order R x C and fill with 0
- q := a double ended queue with some pairs (r, c) where r and c are row and column index of matrix where matrix[r, c] is 0
- if size of q is wither 0 or R * C, then
- return -1

- while q is not empty, do
- (r, c) := left element of q, then remove from q
- for each pair (x, y) in [(r - 1, c) ,(r + 1, c) ,(r, c + 1) ,(r, c - 1) ], do
- if x and y are in range of matrix and A[x, y] is 1, then
- A[x, y] := 0
- distance[x, y] := distance[r, c] + 1
- insert (x, y) at the end of q

- if x and y are in range of matrix and A[x, y] is 1, then

- res := a list of containing maximum element of each rows
- return maximum of res

## Example (Python)

Let us see the following implementation to get better understanding −

from collections import deque class Solution: def solve(self, A): if not A: return 0 R, C = len(A), len(A[0]) distance = [[0] * C for _ in range(R)] q = deque((r, c) for r in range(R) for c in range(C) if not A[r][c]) if len(q) in (0, R * C): return -1 while q: r, c = q.popleft() for x, y in [(r - 1, c), (r + 1, c), (r, c + 1), (r, c - 1)]: if 0 <= x < R and 0 <= y < C and A[x][y]: A[x][y] = 0 distance[x][y] = distance[r][c] + 1 q.append((x, y)) return max(max(row) for row in distance) ob = Solution() matrix = [ [1, 1, 1, 1], [1, 1, 0, 1], [1, 1, 1, 1], [0, 0, 1, 1] ] print(ob.solve(matrix))

## Input

[ [1, 1, 1, 1], [1, 1, 0, 1], [1, 1, 1, 1], [0, 0, 1, 1] ]

## Output

3

- Related Questions & Answers
- Program to find largest island after changing one water cell to land cell in Python
- Program to find longest distance of 1s in binary form of a number using Python
- Program to find longest common prefix from list of strings in Python
- Program to find longest awesome substring in Python
- Program to find largest distance pair from two list of numbers in Python
- Program to find length of longest sublist with given condition in Python
- Program to find length of longest path with even sum in Python
- Program to find length of longest valid parenthesis from given string in Python
- Program to find longest nice substring using Python
- Program to find length of longest substring with even vowel counts in Python
- Program to find length of longest arithmetic subsequence with constant difference in Python
- Program to find length of longest consecutive sublist with unique elements in Python
- Program to find length of longest sublist with value range condition in Python
- Container With Most Water in Python
- Program to find length of longest Fibonacci subsequence from a given list in Python

Advertisements