Jump to content

Algorithmic problem

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Marcinnnn

Marcinnnn

    Newbie

  • Members
  • PipPip
  • 17 posts
Hi,

I have two int variables a and b (defined by a user) and one modification operation for each - simple arithmetic operation like:
a += 2;
b += 3; 
or
a -= 5;
b = b*5 - 3a;
or
a = a^3;
b = b*a - 6b;
etc.

We can change only one variable per step. We have limited number of steps - defined by user. After changing variable we are using its changed value.

Now I want to find sequence of operations which will give me situation when a and b are equal. And I'm interested in shortest way. So I get something like this: [modifyA, modifyA, modifyB, modifyA, modifyB; 5 steps] or [modifyA, modifyA, modifyB, modifyA, modifyB, modifyA, modifyA, modifyB; 8 steps] or [modifyA, modifyA, modifyB, ..., modifyB; 189 steps].

I can do it using recursive algorithm with passing the objects with step and way done but it force me to check every possibility. But with recursive if user will define 10000 steps and quite complicated operations is can be time consuming while the solution can be available after few steps. It's bit like getting binary tree with all possibilities and check each branch until solution will be found. Is there any smart way to check such tree not branch by branch but level by level?

Thanks in advance!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You could encode each operation as a number, and then test arrays of these numbers, "incrementing" as you go. However, it seems like you're looking at a brute force approach, regardless.

Of course, if you do a/=a; b/=b; you'll be there in no time :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users