- 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

# C++ Program to find out the moves to read a point from another point in a 2D plane

Suppose, there are two points in a 2D plane a and b that have the coordinates (x1, y1) and (x2, y2) respectively. Currently, we are at point 'a' and we can move at a distance of 1 either vertically or horizontally. We move to point b from point a, then get back to point a, and we go to point b again. It is not allowed to move through the same points more than once except the points a and b. We have to find out the moves we will make in this whole trip, and output it. If we move right, we print 'R', 'L' if we move left, 'U' if we move up, and 'D' if we move down. One thing we have to note is that x2 > x1 and y2 > y1.

So, if the input is like x1 = 0, y1 = 1, x2 = 3, y2 = 4, then the output will be UUURRRDDDLLLLUUUURRRRDRDDDDLLLLU

To solve this, we will follow these steps −

s := a blank string for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "U" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "R" at the end of s for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "D" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "L" at the end of s add "LU" at the end of s for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "U" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "R" at the end of s add "RD" at the end of s add "RD" at the end of s for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "D" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "L" at the end of s add "LU" at the end of s return s

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; string solve(int x1, int y1, int x2, int y2){ string s = ""; for(int i = 0; i < y2 - y1; i++) s.append("U"); for(int i = 0; i < x2 - x1; i++) s.append("R"); for(int i = 0; i < y2 - y1; i++) s.append("D"); for(int i = 0; i < x2 - x1; i++) s.append("L"); s.append("LU"); for(int i = 0; i < y2 - y1; i++) s.append("U"); for(int i = 0; i < x2 - x1; i++) s.append("R"); s.append("RD"); s.append("RD"); for(int i = 0; i < y2 - y1; i++) s.append("D"); for(int i = 0; i < x2 - x1; i++) s.append("L"); s.append("LU"); return s; } int main() { int x1 = 0, y1 = 1, x2 = 3, y2 = 4; cout<< solve(x1, y1, x2, y2); return 0; }

## Input

0, 1, 3, 4

## Output

UUURRRDDDLLLLUUUURRRRDRDDDDLLLLU

- Related Questions & Answers
- Find foot of perpendicular from a point in 2D plane to a Line in C++
- Finding reflection of a point relative to another point in JavaScript
- Rotation of a point about another point in C++
- Find mirror image of a point in 2-D plane in C++
- Python program to find the Decreasing point in a List
- Program to find out is a point is reachable from the current position through given points in Python
- Find the Number of Ways to go From One Point to Another in a Grid using C++
- Program to find the mid-point of a line in C++
- Program to find the highest altitude of a point in Python
- Find the probability of reaching all points after N moves from point N in C++
- Program to find the Break Even Point in C++
- Point-to-Point Protocol (PPP)
- C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Difference between Point-to-Point and Multi-point Communication