Samkit5025

Untitled

Jun 19th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int countingOperations(int A,int B){
  5. vector<int> dp(2005,-1);
  6. queue<int> q;
  7. q.push(A);
  8. dp[A] = 0;
  9.  
  10. while(!q.empty()){
  11. int temp = q.front();
  12. q.pop();
  13.  
  14. if(temp==B){
  15. return dp[temp];
  16. }
  17.  
  18. if(temp-2>=0 && dp[temp-2]==-1){
  19. q.push(temp-2);
  20. dp[temp-2] = dp[temp]+1;
  21. }
  22. if(temp+5<=(2*B) && dp[temp+5]==-1){
  23. q.push(temp+5);
  24. dp[temp+5] = dp[temp]+1;
  25. }
  26. if(temp<=B && dp[temp*2]==-1){
  27. q.push(temp*2);
  28. dp[temp*2] = dp[temp]+1;
  29. }
  30. }
  31.  
  32. return -1;
  33. }
  34.  
  35. int main()
  36. {
  37. int A,B;
  38. cin>>A>>B;
  39.  
  40. cout<<countingOperations(A,B)<<endl;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment