Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int countingOperations(int A,int B){
- vector<int> dp(2005,-1);
- queue<int> q;
- q.push(A);
- dp[A] = 0;
- while(!q.empty()){
- int temp = q.front();
- q.pop();
- if(temp==B){
- return dp[temp];
- }
- if(temp-2>=0 && dp[temp-2]==-1){
- q.push(temp-2);
- dp[temp-2] = dp[temp]+1;
- }
- if(temp+5<=(2*B) && dp[temp+5]==-1){
- q.push(temp+5);
- dp[temp+5] = dp[temp]+1;
- }
- if(temp<=B && dp[temp*2]==-1){
- q.push(temp*2);
- dp[temp*2] = dp[temp]+1;
- }
- }
- return -1;
- }
- int main()
- {
- int A,B;
- cin>>A>>B;
- cout<<countingOperations(A,B)<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment