Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
What would be the algorithm for following question- 
If you are given input(a,b) is it possible to reach(c,d) i.e from (a,b) you can go (a+b,b) or (a,a+b) n number of times .Answers should be in ‘yes’ or ‘No’.
 eg- i.e if ur (a,b) is -(1,4) it can move to (5,4)==>> (5,9)==>>(5,14) and so on. 
Java Syntax-
 String is Possible(int a, int b, int c, int d). { }


What I have tried:

I think graph will help , but I am not sure about it .
Posted
Updated 25-Sep-16 11:47am
Comments
[no name] 25-Sep-16 13:46pm    
That depends. What were you studying in class when you got this homework assignment?

(x0, y0) can reach (x1, y1) if x0 can reach x1 and y0 can reach y1, assuming x0 <= x1 and y0 <= y1, then using bool to indicate whether (x0, y0) can reach (x1, y1) the following might do it

C#
using System;

namespace CanReach
{
    internal class Program
    {
        public static int CanReachOne(int x, int y, int tx)
        {
            int f = 0;
            while (x < tx)
            {
                x += y;
                f++;
            }
            return x == tx ? f : -1;
        }

        public static bool CanReach(int a, int b, int c, int d)
        {
            int aCan = CanReachOne(a, b, c);
            int bCan = CanReachOne(b, a, d);
            return aCan > -1 && bCan > -1;
        }

        private static void Main(string[] args)
        {

            Console.WriteLine("({0},{1}) => ({2},{3}) = {4}", 1, 4, 5, 14, 
                              CanReach(1, 4, 5, 14));
            Console.ReadLine();
        }
    }
}
 
Share this answer
 
Comments
Patrice T 25-Sep-16 15:32pm    
I fear this answer is wrong.
From (1,2), try to reach (8,13)
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
Quote:
I think graph will help , but I am not sure about it .
If a graph can help, you should know why, otherwise it is a guess.
Look at the statements: you are in map at coordinates (a,b) and you need to reach coordinates (c,d) on each steps, you are only allowed 2 moves. Try to put this together.

For testing purpose, try to go from (1,2) to (8,13)
The path is (1,2) => (3,2) => (3,5) => (8,5) => (8,13)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900