To the Magic Show: The Magic Castle, the home of the Academy of Magical Arts has organized the great ‘WonderWorks Magic Show’. Renowned magicians were invited to mystify and thrill the crowd with their world’s spectacular magic tricks. Rahul has promised to take his little kids Akash and Anusha to this renownedshow.
Unfortunately Rahul got a bit late to return from work, but he did not want to disappoint the kids.So he started off with themto the show in his car.He thought to take a short route to reach the show on time but he was not sure if it is possible to reach the magic show venue at the point (x2, y2) from his house which is at a point (x1, y1).
Given coordinates of a source point (x1, y1), write a recursive method to determine if it is possible to reach the destination point (x2, y2).
Note: From any point (x, y) there only two types of valid movements: (x, x + y) and (x + y, y).
Hence create a class named Reachable with the following method.
Method Name | Description |
int isReachable(int,int,int,int) | This recursive method return 1 if the given source point will reach the destination point. Else return 0. |
Create a driver class called Main. In the Main method, obtain input from the user in the console and display “True” if the given source point will reach the destination point, else display “False” by calling the isReachable method present in the Reachable class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement.]
Input Format:
First line of the input contains the source point (x1, y1).
Second line of the input contains the source point (x2, y2).
Output Format:
Output “True” (without quotes) if the given source point will reach the destination point. Print “False” (without quotes) otherwise.
Refer sample input and output for formatting specifications.
Sample Input 1:
2 10
26 12
Sample Output 1:
True
Sample Input 2:
20 10
6 12
Sample Output 2:
False
Solution
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int s_x = sc.nextInt();
int s_y = sc.nextInt();
int d_x = sc.nextInt();
int d_y = sc.nextInt();
int isreach = isReachable(s_x,s_y,d_x,d_y);
if(isreach==1)
System.out.println("True");
else
System.out.println("False");
}
public static int isReachable (int s_x,int s_y,int d_x,int d_y)
{
if(s_x>d_x || s_y>d_y)
return 0;
if(s_x==d_x && s_y==d_y)
return 1;
return isReachable(s_x,s_x+s_y,d_x,d_y)|isReachable(s_x+s_y, s_y, d_x,d_y);
}
}
#include <bits/stdc++.h>
using namespace std;
bool isReachable(int sx, int sy, int dx, int dy)
{
if (sx > dx || sy > dy)
return false;
if (sx == dx && sy == dy)
return true;
return (isReachable(sx + sy, sy, dx, dy) ||
isReachable(sx, sy + sx, dx, dy));
}
int main()
{
int source_x, source_y;
int dest_x,dest_y;
cin >> source_x;
cin >> source_y;
cin >> dest_x;
cin >> dest_y;
if (isReachable(source_x, source_y, dest_x, dest_y))
cout << "True\n";
else
cout << "False\n";
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.