Check if it is possible to move from X to Z using point Y
Last Updated : 24 Apr, 2023
Given three points X, Y, and Z of a 2D plane, the task is to check if it is possible to move from X to Z using point Y with at most one turn of 90 degrees. If possible return "YES" else return "NO".
Note: Diagonal moves are not allowed, only vertical or horizontal moves are allowed.
Examples:
Input: X = {1, 3}, Y{4, 3}, Z = {4, 5} Output: YES Explanation:
Explanation of first test case
Input: X = {1, 1}, Y = {2, 2}, Z = {3, 3} Output: NO Explanation: It can be verified that we can't reach point Z from X using point Y.
Brute Force Approach :
We can check all possible paths from X to Z that involve at most one turn of 90 degrees at Y. We can start by moving from X to Y in a straight line, and then from Y to Z in a straight line, and check if this path is valid. If not, we can check all paths that involve one turn of 90 degrees at Y by considering all possible directions of the turn (left, right, up, or down), and checking if each path is valid. If we find a valid path, we can return "YES", otherwise we return "NO".
Below is the Implementation of the above approach :
C++
#include<iostream>usingnamespacestd;structPoint{intx,y;};// function to check if two points are on the same horizontal or vertical lineboolisOnLine(Pointp1,Pointp2){return(p1.x==p2.x||p1.y==p2.y);}// function to check if it is possible to move from point X to point Z// using point Y with at most one turn of 90 degreesstringisPossible(PointX,PointY,PointZ){// check if there is a straight line path from X to Zif(isOnLine(X,Z)){return"YES";}// check all possible paths that involve one turn of 90 degrees at Y// by considering all possible directions of the turnfor(intdx=-1;dx<=1;dx++){for(intdy=-1;dy<=1;dy++){// check if the turn direction is validif(dx==0||dy==0||dx==dy||dx==-dy){// calculate the intermediate point after the turnPointp={Y.x+dx,Y.y+dy};// check if the path from X to p and from p to Z is validif(isOnLine(X,p)&&isOnLine(p,Z)){return"YES";}}}}// if no valid path is found, return "NO"return"NO";}intmain(){PointX={1,3};// example pointsPointY={4,3};PointZ={4,5};cout<<isPossible(X,Y,Z)<<endl;// output: YESreturn0;}
Java
publicclassMain{staticclassPoint{intx,y;publicPoint(intx,inty){this.x=x;this.y=y;}}// function to check if two points are on the same horizontal or vertical linestaticbooleanisOnLine(Pointp1,Pointp2){return(p1.x==p2.x||p1.y==p2.y);}// function to check if it is possible to move from point X to point Z// using point Y with at most one turn of 90 degreesstaticStringisPossible(PointX,PointY,PointZ){// check if there is a straight line path from X to Zif(isOnLine(X,Z)){return"YES";}// check all possible paths that involve one turn of 90 degrees at Y// by considering all possible directions of the turnfor(intdx=-1;dx<=1;dx++){for(intdy=-1;dy<=1;dy++){// check if the turn direction is validif(dx==0||dy==0||dx==dy||dx==-dy){// calculate the intermediate point after the turnPointp=newPoint(Y.x+dx,Y.y+dy);// check if the path from X to p and from p to Z is validif(isOnLine(X,p)&&isOnLine(p,Z)){return"YES";}}}}// if no valid path is found, return "NO"return"NO";}publicstaticvoidmain(String[]args){PointX=newPoint(1,3);// example pointsPointY=newPoint(4,3);PointZ=newPoint(4,5);System.out.println(isPossible(X,Y,Z));// output: YES}}// This code is contributed by Prajwal Kandekar
Python3
classPoint:def__init__(self,x,y):self.x=xself.y=y# function to check if two points are on the same horizontal or vertical line defisOnLine(p1,p2):return(p1.x==p2.xorp1.y==p2.y)# function to check if it is possible to move from point X to point Z# using point Y with at most one turn of 90 degreesdefisPossible(X,Y,Z):# check if there is a straight line path from X to ZifisOnLine(X,Z):return"YES"# check all possible paths that involve one turn of 90 degrees at Y# by considering all possible directions of the turnfordxinrange(-1,2):fordyinrange(-1,2):# check if the turn direction is validifdx==0ordy==0ordx==dyordx==-dy:# calculate the intermediate point after the turnp=Point(Y.x+dx,Y.y+dy)# check if the path from X to p and from p to Z is validifisOnLine(X,p)andisOnLine(p,Z):return"YES"# if no valid path is found, return "NO" return"NO"X=Point(1,3)# example pointsY=Point(4,3)Z=Point(4,5)print(isPossible(X,Y,Z))# output: YES
C#
usingSystem;publicclassProgram{staticvoidMain(string[]args){PointX=newPoint(1,3);// example pointsPointY=newPoint(4,3);PointZ=newPoint(4,5);Console.WriteLine(isPossible(X,Y,Z));// output: YES}classPoint{publicintx,y;publicPoint(intx,inty){this.x=x;this.y=y;}}// function to check if two points are on the same// horizontal or vertical linestaticboolisOnLine(Pointp1,Pointp2){return(p1.x==p2.x||p1.y==p2.y);}// function to check if it is possible to move from// point X to point Z using point Y with at most one// turn of 90 degreesstaticstringisPossible(PointX,PointY,PointZ){// check if there is a straight line path from X to// Zif(isOnLine(X,Z)){return"YES";}// check all possible paths that involve one turn of// 90 degrees at Y by considering all possible// directions of the turnfor(intdx=-1;dx<=1;dx++){for(intdy=-1;dy<=1;dy++){// check if the turn direction is validif(dx==0||dy==0||dx==dy||dx==-dy){// calculate the intermediate point// after the turnPointp=newPoint(Y.x+dx,Y.y+dy);// check if the path from X to p and// from p to Z is validif(isOnLine(X,p)&&isOnLine(p,Z)){return"YES";}}}}// if no valid path is found, return "NO"return"NO";}}// This code is contributed by Tapesh(tapeshdua420)
JavaScript
functionPoint(x,y){this.x=x;this.y=y;}// function to check if two points are // on the same horizontal or vertical linefunctionisOnLine(p1,p2){return(p1.x==p2.x||p1.y==p2.y);}// function to check if it is possible to move from point X to point Z// using point Y with at most one turn of 90 degreesfunctionisPossible(X,Y,Z){// check if there is a straight line path from X to Zif(isOnLine(X,Z)){return"YES";}// check all possible paths that involve one turn of 90 degrees at Y// by considering all possible directions of the turnfor(letdx=-1;dx<=1;dx++){for(letdy=-1;dy<=1;dy++){// check if the turn direction is validif(dx==0||dy==0||dx==dy||dx==-dy){// calculate the intermediate point after the turnletp=newPoint(Y.x+dx,Y.y+dy);// check if the path from X to p and from p to Z is validif(isOnLine(X,p)&&isOnLine(p,Z)){return"YES";}}}}// if no valid path is found, return "NO"return"NO";}letX=newPoint(1,3);// example pointsletY=newPoint(4,3);letZ=newPoint(4,5);console.log(isPossible(X,Y,Z));// output: YES
Output :
YES
Complexity Analysis :
The time complexity of this solution is O(1) for the initial check if X and Z are on the same line, and O(9) for the nested loops that consider all possible paths with one turn at Y. Therefore, the overall time complexity is O(1) + O(9) = O(1).
The auxiliary space of this solution is O(1) as it only uses a constant amount of additional space to store the three points and the intermediate point after the turn.
Approach: Implement the idea below to solve the problem:
The problem is observation based and can be solved via implementing those observations. For more clarification see the Concept of approach section below.
Concept of approach:
It should be noted that Reaching from X to Z is only possible when Y is an intermediate point between X and Z. We can move in the horizontal or vertical direction, So that either the Y should be in between x coordinate of X and Z or y coordinate of X and Z also. So the conditions at which reaching is possible are:
else if (y2 ? min(y1, y3) && y2 ? max(y1, y3) && (x2 == x1 || x2 == x3)), Only at these conditions a path will exist.
Otherwise, no path is possible.
Below is the Implementation of the above approach:
C++
// C++ code to implement the approach#include<algorithm>#include<iostream>usingnamespacestd;intmain(){// Inputs of X, Y and Zlongx1=1,x2=2,x3=2,y1=4,y2=4,y3=5;// Minimum X coordinate from x1 and x3longsmallx=min(x1,x3);// Maximum X coordinate from x1 and x3longbigx=max(x1,x3);// Minimum Y coordinate from y1 and y3longbigy=max(y1,y3);// Maximum Y coordinate from y1 and y3longsmally=min(y1,y3);// Checking for conditionsif(x2>=smallx&&x2<=bigx&&(y2==y1||y2==y3)){cout<<"YES"<<endl;}elseif(y2>=smally&&y2<=bigy&&(x2==x1||x2==x3)){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}return0;}
Java
// Java code to implement the approachpublicclassGFG{// Driver Functionpublicstaticvoidmain(String[]args){// Inputs of X, Y and Zlongx1=1,x2=2,x3=2,y1=4,y2=4,y3=5;// Minimum X coordinate from x1 and x3longsmallx=Math.min(x1,x3);// Maximum X coordinate from x1 and x3longbigx=Math.max(x1,x3);// Minimum Y coordinate from y1 and y3longbigy=Math.max(y1,y3);// Maximum Y coordinate from y1 and y3longsmally=Math.min(y1,y3);// Checking for conditionsif(x2>=smallx&&x2<=bigx&&(y2==y1||y2==y3)){System.out.println("YES");}elseif(y2>=smally&&y2<=bigy&&(x2==x1||x2==x3)){System.out.println("YES");}elseSystem.out.println("NO");}}
Python3
# Python code to implement the approachimportsys# Inputs of X, Y and Zx1,x2,x3,y1,y2,y3=1,2,2,4,4,5# Minimum X coordinate from x1 and x3smallx=min(x1,x3)# Maximum X coordinate from x1 and x3bigx=max(x1,x3)# Minimum Y coordinate from y1 and y3bigy=max(y1,y3)# Maximum Y coordinate from y1 and y3smally=min(y1,y3)# Checking for conditionsifx2>=smallxandx2<=bigxand(y2==y1ory2==y3):print("YES")elify2>=smallyandy2<=bigyand(x2==x1orx2==x3):print("YES")else:print("NO")# This code is contributed by prasad264
C#
// C# code to implement the approachusingSystem;publicclassGFG{publicstaticvoidMain(string[]args){// Inputs of X, Y and Zlongx1=1,x2=2,x3=2,y1=4,y2=4,y3=5;// Minimum X coordinate from x1 and x3longsmallx=Math.Min(x1,x3);// Maximum X coordinate from x1 and x3longbigx=Math.Max(x1,x3);// Minimum Y coordinate from y1 and y3longbigy=Math.Max(y1,y3);// Maximum Y coordinate from y1 and y3longsmally=Math.Min(y1,y3);// Checking for conditionsif(x2>=smallx&&x2<=bigx&&(y2==y1||y2==y3)){Console.WriteLine("YES");}elseif(y2>=smally&&y2<=bigy&&(x2==x1||x2==x3)){Console.WriteLine("YES");}else{Console.WriteLine("NO");}}}// This code is contributed by prasad264
JavaScript
// Inputs of X, Y and Zletx1=1,x2=2,x3=2,y1=4,y2=4,y3=5;// Minimum X coordinate from x1 and x3letsmallx=Math.min(x1,x3);// Maximum X coordinate from x1 and x3letbigx=Math.max(x1,x3);// Minimum Y coordinate from y1 and y3letbigy=Math.max(y1,y3);// Maximum Y coordinate from y1 and y3letsmally=Math.min(y1,y3);// Checking for conditionsif(x2>=smallx&&x2<=bigx&&(y2==y1||y2==y3)){console.log("YES");}elseif(y2>=smally&&y2<=bigy&&(x2==x1||x2==x3)){console.log("YES");}else{console.log("NO");}