
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- ‘Avoid plastics as far as possible’. Comment on this advice.
- How do you make a separation and divorce as amicable as possible?
- Underscore as a table name in MySQL is possible?
- Possible two sets from first N natural numbers difference of sums as D in C++
- Is it possible to use MongoDB field value as pattern in $regex?
- Is it possible to use MongoDB field value as a pattern in $regex?
- Represent a Number as Sum of Minimum Possible Pseudo-Binary Numbers in C++
- Express 18 as the sum of two prime numbers in all possible ways.
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- Retrieve time from MySQL as HH:MM format?
- The chemical reaction involved in the corrosion of iron metal is that of:(a)oxidation as well as displacement(b) reduction as well as combination(c) oxidation as well as combination(d)reduction as well as displacement
- Which of the following can make a parallel beam of light when light from a bulb falls on it?(a) concave mirror as well as concave lens (b) convex mirror as well as convex lens(c) concave mirror as well as convex lens (d) convex mirror as well as concave lens
- Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?
- How is it possible to store date such as February 30 in a MySQL date column?
- C# Program to skip elements from a sequence as long as the specified condition is true
