Create a gist now

Instantly share code, notes, and snippets.

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int A, B, C, ans = 999999999;
int solve(int num,int cnt){
if (num == C){
ans = min(ans, cnt);
return 0;
}
if (cnt == 10)return 0;
if (num > C){
solve(num + 1,cnt+1);
solve(num - 1, cnt + 1);
solve(num + 5, cnt + 1);
solve(num - 5, cnt + 1);
solve(num + 10, cnt + 1);
solve(num - 10, cnt + 1);
}
else{
solve(num + 10, cnt + 1);
solve(num + 5, cnt + 1);
solve(num + 1, cnt + 1);
}
return 0;
}
int main(){
cin >> A >> B;
C = abs(B - A);
solve(0, 0);
cout << ans << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment