
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
As Far From Land As Possible in C++
Suppose we have one N x N grid containing only values like 0 and 1, where 0 represents water and 1 represents the land, we have to find a water cell such that its distance to the nearest land cell is maximized and return the distance. Here we will use the Manhattan distance − the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|. If no land or water is present in the grid, then return -1.
1 | 0 | 1 |
0 | 0 | 0 |
1 | 0 | 1 |
Then the output will be 2, as the cell (1,1) is as far as possible from all the land with distance 2.
To solve this, we will follow these steps −
dir := [(1, 0), (-1, 0), (1, -1), (1, 1), (-1, 1), (-1, -1), (0, 1), (0, -1)]
dir2 := [(1, 0), (-1, 0), (0, 1), (0, -1)]
Define a map m. Define a queue q. n := row count and c := column count
-
for i in range 0 to n – 1
-
for j in range 0 to n – 1
if grid[i, j] is 1, then insert a pair (i, j) into q and put m[(i, j)] := (j ,i)
-
ret := -1
-
while the q is not empty
sz := size of q
-
while sz is not 0
temp := first element of q, delete first element from q
-
for k in range 0 to 3 −
nx := first value of temp + dir2[k, 0]
ny := second value of temp + dir2[k, 1]
if nx and ny are not in range of grid, or grid[nx, ny] is 1, then skip to the next iteration.
m[(nx, ny)] := m[temp]
ret := max of (distance of (nx, ny) and m(temp)) and ret
insert (nx,ny) into q
set grid[nx, ny] := 1
decrease sz by 1
return ret
Example(C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int dir[8][2] = { {1, 0}, {-1, 0}, {1, -1}, {1, 1}, {-1, 1}, {-1, -1}, {0, 1}, {0, -1} }; int dir2[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; class Solution { public: int calcDist(int x1, int y1, int x2, int y2){ return abs(x1 - x2) + abs(y1 - y2); } int maxDistance(vector<vector<int>>& grid) { map < pair <int, int>, pair <int, int> > m; queue < pair <int, int> > q; int n = grid.size(); int c = n? grid[0].size() : 0; for(int i = 0; i < n; i++){ for(int j = 0; j < c; j++){ if(grid[i][j] == 1){ q.push({i, j}); m[{i, j}] = {i, j}; } } } int ret = -1; while(!q.empty()){ int sz = q.size(); while(sz--){ pair <int, int> temp = q.front(); q.pop(); for(int k = 0; k < 4; k++){ int nx = temp.first + dir2[k][0]; int ny = temp.second + dir2[k][1]; if(nx < 0 || ny < 0 || nx >= n || ny >= c || grid[nx][ny]) continue; m[{nx, ny}] = m[temp]; ret = max(calcDist(nx, ny, m[temp].first, m[temp].second), ret); q.push({nx, ny}); grid[nx][ny] = 1; } } } return ret; } }; main(){ vector<vector<int>> v1 = {{1,0,1},{0,0,0},{1,0,1}}; Solution ob; cout << (ob.maxDistance(v1)); }
Input
["alice,20,800,mtv","bob,50,1200,mtv"]
Output
2