# Find single Movement in a Matrix

Given four integers x1, y1 and x2, y2 which represent two locations in an infinite 2D-Matrix, the task is to find whether it is possible to move from (x1, y1) to (x2, y2) in a single move, either left, right, up or down. Note that the move will be repeated until the destination is reached. If it is impossible to reach (x2, y2) output -1.

Examples:

Input: x1 = 0, y1 = 0, x2 = 1, y2 = 0
Output: Down
Destination is just below the starting point.

Input: x1 = 0, y1 = 0, x2 = 1, y2 = 1
Output: -1
It is impossible to reach (1, 1) from (0, 0) in a single move.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Check if the coordinates are either in the same row or in the same column then only its possible to reach the final destination. Then print the move according to the direction of the destination.

Below is the implementation of the above approach:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// Function that checks whether it is ` `// possible to move from ` `// (x1, y1) to (x2, y2) ` `void` `Robot_Grid(``int` `x1, ``int` `y1, ``int` `x2, ``int` `y2) ` `{ ` `    ``// Both locations are ` `    ``// in the same row ` `    ``if` `(x1 == x2) { ` ` `  `        ``// Destination is ` `        ``// at the right ` `        ``if` `(y1 < y2) { ` `            ``cout << ``"Right"``; ` `        ``} ` `        ``// Destination is ` `        ``// at the left ` `        ``else` `{ ` `            ``cout << ``"Left"``; ` `        ``} ` `    ``} ` ` `  `    ``// Both locations are ` `    ``// in the same column ` `    ``else` `if` `(y1 == y2) { ` ` `  `        ``// Destination is below ` `        ``// the current row ` `        ``if` `(x1 < x2) { ` `            ``cout << ``"Down"``; ` `        ``} ` ` `  `        ``// Destination is above ` `        ``// the current row ` `        ``else` `{ ` `            ``cout << ``"Up"``; ` `        ``} ` `    ``} ` ` `  `    ``// Impossible to get ` `    ``// to the destination ` `    ``else` `{ ` `        ``cout << ``"-1"``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `x1, x2, y1, y2; ` `    ``x1 = 0; ` `    ``y1 = 0; ` `    ``x2 = 0; ` `    ``y2 = 1; ` ` `  `    ``Robot_Grid(x1, y1, x2, y2); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the given above approach ` ` `  `public` `class` `GFG{ ` ` `  `    ``// Function that checks whether it is  ` `    ``// possible to move from  ` `    ``// (x1, y1) to (x2, y2)  ` `    ``static` `void` `Robot_Grid(``int` `x1, ``int` `y1, ``int` `x2, ``int` `y2)  ` `    ``{  ` `        ``// Both locations are  ` `        ``// in the same row  ` `        ``if` `(x1 == x2) {  ` `     `  `            ``// Destination is  ` `            ``// at the right  ` `            ``if` `(y1 < y2) {  ` `                ``System.out.print(``"Right"``);  ` `            ``}  ` `            ``// Destination is  ` `            ``// at the left  ` `            ``else` `{  ` `                ``System.out.print(``"Left"``);  ` `            ``}  ` `        ``}  ` `     `  `        ``// Both locations are  ` `        ``// in the same column  ` `        ``else` `if` `(y1 == y2) {  ` `     `  `            ``// Destination is below  ` `            ``// the current row  ` `            ``if` `(x1 < x2) {  ` `                ``System.out.print(``"Down"``); ` `            ``}  ` `     `  `            ``// Destination is above  ` `            ``// the current row  ` `            ``else` `{  ` `                ``System.out.println(``"Up"``);  ` `            ``}  ` `        ``}  ` `     `  `        ``// Impossible to get  ` `        ``// to the destination  ` `        ``else` `{  ` `            ``System.out.print(``"-1"``);  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `     ``public` `static` `void` `main(String []args) ` `    ``{  ` `        ``int` `x1, x2, y1, y2;  ` `        ``x1 = ``0``;  ` `        ``y1 = ``0``;  ` `        ``x2 = ``0``;  ` `        ``y2 = ``1``;  ` `     `  `        ``Robot_Grid(x1, y1, x2, y2);  ` `}  ` ` `  `// This code is contributed by Ryuga ` `} `

## Python3

 `# Function that checks whether it is ` `# possible to move from ` `# (x1, y1) to (x2, y2) ` ` `  `def` `Robot_Grid(x1, y1, x2, y2): ` `     `  `    ``# Both locations are in the same row ` `    ``if` `(x1 ``=``=` `x2): ` `         `  `        ``# Destination is at the right ` `        ``if` `(y1 < y2): ` `            ``print``(``"Right"``) ` ` `  `        ``# Destination is at the left ` `        ``else``: ` `            ``print``(``"Left"``) ` `         `  `    ``# Both locations are in the same column ` `    ``elif` `(y1 ``=``=` `y2): ` `         `  `        ``# Destination is below the current row ` `        ``if` `(x1 < x2): ` `            ``print``(``"Down"``) ` ` `  `        ``# Destination is above the current row ` `        ``else``: ` `            ``print``(``"Up"``) ` ` `  `    ``# Impossible to get to the destination ` `    ``else``: ` `        ``print``(``"-1"``) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``x1 ``=` `0` `    ``y1 ``=` `0` `    ``x2 ``=` `0` `    ``y2 ``=` `1` ` `  `    ``Robot_Grid(x1, y1, x2, y2) ` ` `  `# This code is contributed by ` `# Sanjit_Prasad `

## C#

 `// C# implementation of the given above approach ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `    ``// Function that checks whether it is  ` `    ``// possible to move from  ` `    ``// (x1, y1) to (x2, y2)  ` `    ``static` `void` `Robot_Grid(``int` `x1, ``int` `y1,  ` `                           ``int` `x2, ``int` `y2)  ` `    ``{  ` `        ``// Both locations are  ` `        ``// in the same row  ` `        ``if` `(x1 == x2)  ` `        ``{  ` `     `  `            ``// Destination is  ` `            ``// at the right  ` `            ``if` `(y1 < y2)  ` `            ``{  ` `                ``Console.Write(``"Right"``);  ` `            ``}  ` `            ``// Destination is  ` `            ``// at the left  ` `            ``else`  `            ``{  ` `                ``Console.Write(``"Left"``);  ` `            ``}  ` `        ``}  ` `     `  `        ``// Both locations are  ` `        ``// in the same column  ` `        ``else` `if` `(y1 == y2)  ` `        ``{  ` `     `  `            ``// Destination is below  ` `            ``// the current row  ` `            ``if` `(x1 < x2)  ` `            ``{  ` `                ``Console.Write(``"Down"``); ` `            ``}  ` `     `  `            ``// Destination is above  ` `            ``// the current row  ` `            ``else`  `            ``{  ` `                ``Console.WriteLine(``"Up"``);  ` `            ``}  ` `        ``}  ` `     `  `        ``// Impossible to get  ` `        ``// to the destination  ` `        ``else`  `        ``{  ` `            ``Console.WriteLine(``"-1"``);  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main() ` `    ``{  ` `        ``int` `x1, x2, y1, y2;  ` `        ``x1 = 0;  ` `        ``y1 = 0;  ` `        ``x2 = 0;  ` `        ``y2 = 1;  ` `     `  `        ``Robot_Grid(x1, y1, x2, y2);  ` `    ``}  ` `} ` ` `  `// This code is contributed by  ` `// Mukul Singh `

## PHP

 ` `

Output:

```Right
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.