
- 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
Program to Find the Shortest Distance Between Two Points in C++
Suppose we have a list of coordinates where each element is of the form [x, y], representing Euclidean coordinates. We have to find the smallest squared distance (x1 - x2) 2 + (y1 - y2) 2 between any two provided coordinates.
So, if the input is like coordinates = {{1, 2},{1, 4},{3, 5}}, then the output will be 4.
To solve this, we will follow these steps −
Define one map ytorightmostx
sort the array coordinates
ret := infinity
for each p in cordinates −
it = return the value where (p[1] - sqrt(ret)) is in ytorightmostx or the smallest value greater than it from ytorightmostx
while it is not equal to last element of ytorightmostx, do −
yd := first - p[1] of it
if yd > 0 and yd * yd >= ret, then −
Come out from the loop
nxt = it
increase nxt by 1
ret := minimum of (ret, dist(p[0], p[1], first value of it, second value of it)
xd := second value of it - p[0]
if xd * xd >= ret, then −
delete it from ytorightmostx
it := nxt
ytorightmostx[p[1]] := p[0]
return ret
Define a function dist(), this will take xl, yl, xr, yr.
xd := xl - xr
yd := yl - yr
return xd * xd + yd * yd
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; long long dist(long long xl, long long yl, long long xr, long long yr) { long long xd = xl - xr; long long yd = yl - yr; return xd * xd + yd * yd; } int solve(vector<vector<int>>& coordinates) { map<long long, long long> ytorightmostx; sort(coordinates.begin(), coordinates.end()); long long ret = 1e18; for (auto& p : coordinates) { auto it = ytorightmostx.lower_bound(p[1] - sqrt(ret)); while (it != ytorightmostx.end()) { long long yd = it->first - p[1]; if (yd > 0 && yd * yd >= ret) { break; } auto nxt = it; nxt++; ret = min(ret, dist(p[0], p[1], it->second, it->first)); long long xd = (it->second - p[0]); if (xd * xd >= ret) { ytorightmostx.erase(it); } it = nxt; } ytorightmostx[p[1]] = p[0]; } return ret; } int main(){ vector<vector<int>> coord = {{1, 2},{1, 4},{3, 5}}; cout << solve(coord) << endl; return 0; }
Input
{{1, 2},{1, 4},{3, 5}}
Output
4
- Related Articles
- C++ program to find the shortest distance between two nodes in BST
- C program to calculate distance between two points
- Swift Program to Calculate Distance Between Two Points
- Program to find distance of shortest bridge between islands in Python
- C++ Program to find the Shortest Distance to a character
- Find the shortest distance between any pair of two different good nodes in C++
- Shortest distance between objects in JavaScript
- Program to find the minimum edit distance between two strings in C++
- C program to calculate distance between three points in 3D
- OpenCV Python – How to find the shortest distance between a point in the image and a contour?
- Find the distance between the 2 points (3,2) and (2,3).
- Program to find out distance between two nodes in a binary tree in Python
- Find the distance between the points $( 0, 0)$ and $( 36, 15)$.
- Find distance between two nodes of a Binary Tree in C++ Program
- Find the minimum distance between two numbers in C++
