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.
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 <bits/stdc++.h>
using namespace std;
void Robot_Grid( int x1, int y1, int x2, int y2)
{
if (x1 == x2) {
if (y1 < y2) {
cout << "Right" ;
}
else {
cout << "Left" ;
}
}
else if (y1 == y2) {
if (x1 < x2) {
cout << "Down" ;
}
else {
cout << "Up" ;
}
}
else {
cout << "-1" ;
}
}
int main()
{
int x1, x2, y1, y2;
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 1;
Robot_Grid(x1, y1, x2, y2);
return 0;
}
|
Java
public class GFG{
static void Robot_Grid( int x1, int y1, int x2, int y2)
{
if (x1 == x2) {
if (y1 < y2) {
System.out.print( "Right" );
}
else {
System.out.print( "Left" );
}
}
else if (y1 == y2) {
if (x1 < x2) {
System.out.print( "Down" );
}
else {
System.out.println( "Up" );
}
}
else {
System.out.print( "-1" );
}
}
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);
}
}
|
Python3
def Robot_Grid(x1, y1, x2, y2):
if (x1 = = x2):
if (y1 < y2):
print ( "Right" )
else :
print ( "Left" )
elif (y1 = = y2):
if (x1 < x2):
print ( "Down" )
else :
print ( "Up" )
else :
print ( "-1" )
if __name__ = = '__main__' :
x1 = 0
y1 = 0
x2 = 0
y2 = 1
Robot_Grid(x1, y1, x2, y2)
|
C#
using System;
class GFG{
static void Robot_Grid( int x1, int y1,
int x2, int y2)
{
if (x1 == x2)
{
if (y1 < y2)
{
Console.Write( "Right" );
}
else
{
Console.Write( "Left" );
}
}
else if (y1 == y2)
{
if (x1 < x2)
{
Console.Write( "Down" );
}
else
{
Console.WriteLine( "Up" );
}
}
else
{
Console.WriteLine( "-1" );
}
}
public static void Main()
{
int x1, x2, y1, y2;
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 1;
Robot_Grid(x1, y1, x2, y2);
}
}
|
PHP
<?php
function Robot_Grid( $x1 , $y1 , $x2 , $y2 )
{
if ( $x1 == $x2 )
{
if ( $y1 < $y2 )
{
echo "Right" ;
}
else
{
echo "Left" ;
}
}
else if ( $y1 == $y2 )
{
if ( $x1 < $x2 )
{
echo "Down" ;
}
else
{
echo "Up" ;
}
}
else
{
echo "-1" ;
}
}
$x1 = 0;
$y1 = 0;
$x2 = 0;
$y2 = 1;
Robot_Grid( $x1 , $y1 , $x2 , $y2 );
?>
|
Javascript
<script>
function Robot_Grid(x1,y1,x2,y2)
{
if (x1 == x2) {
if (y1 < y2) {
document.write( "Right" );
}
else {
document.write( "Left" );
}
}
else if (y1 == y2) {
if (x1 < x2) {
document.write( "Down" );
}
else {
document.write( "Up" );
}
}
else {
document.write( "-1" );
}
}
let x1, x2, y1, y2;
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 1;
Robot_Grid(x1, y1, x2, y2);
</script>
|
Time Complexity: O(1), Only constant time operations are performed.
Auxiliary Space: O(1), As constant extra space is used.
Last Updated :
06 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...