/* file : pathLeastResistance.c */ /* author : Oscar MirĂ³ (o.miro.i.lopez.feliu@student.rug.nl) */ /* Description: * Given two numbers, find the minimum number of steps to go from the first to the * second, using five different operations. */ #include // recursive func int check (int start, int dest, int count, int lim) { // if the final number has been reached, or the theoretical limit if (start == dest || count == lim) { return count; } // idea of the code is to have an index per possible operation // every operation will branch out and repeat the process // every branch will repeatedly branch out till it reaches the theoretical max limit // or until it reaches the final word // every branch will then return the smallest count obtained till on the initial branch // it returns the smallest count to the main func int method[5]; // sets all index to the max limit, in case they can not make an operation as not // divisible by 3 for (int i = 0; i < 5; i++) { method[i] = lim; } // branching out method[0] = check(start+1, dest, count+1, lim); method[1] = check(start-1, dest, count+1, lim); method[2] = check(start*5, dest, count+1, lim); // only if possible if (start % 2 == 0) { method[3] = check(start/2, dest, count+1, lim); } if (start % 3 == 0) { method[4] = check(start/3, dest, count+1, lim); } // finds the smallest count among all the returned values int min = lim; for (int i = 0; i < 5; i++) { if (min > method[i]) { min = method[i]; } } // returns the smallest count return min; } // declaration, assignation and printing int main (int argc, char *argv[]) { int start, dest; scanf("%d %d", &start, &dest); printf("%d\n", check(start, dest, 0, abs(dest-start))); }