Advertisement
Josif_tepe

Untitled

Mar 18th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include<vector>
  3. #include<queue>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int s,e;
  9. cin>>s>>e;
  10. int razlika=e-s;
  11. int palindromi[e-s+1];
  12. int brojac=0;
  13. for(int i=1;i<=(e-s);i++)
  14. {
  15. int e_palindrom = 1;
  16. vector<int> v;
  17. int tmp = i;
  18. while(tmp > 0) {
  19. v.push_back(tmp % 10);
  20. tmp /= 10;
  21. }
  22. int a = 0, b = v.size() - 1;
  23. while(a < b) {
  24. if(v[a] != v[b]) {
  25. e_palindrom = 0;
  26. break;
  27. }
  28. a += 1;
  29. b -= 1;
  30. }
  31. if(e_palindrom==1)
  32. {
  33. palindromi[brojac]=i;
  34. brojac++;
  35. }
  36. }
  37. queue<int>q;
  38. q.push(s);
  39. q.push(0);
  40. int visited[e+1];
  41. for(int i=0;i<e+1;i++)
  42. {
  43. visited[i]=false;
  44. }
  45. while(!q.empty())
  46. {
  47. int ci=q.front();
  48. q.pop();
  49. int cekor=q.front();
  50. q.pop();
  51. if(ci==e)
  52. {
  53. cout<<cekor<<endl;
  54. return 0;
  55. }
  56. for(int k=0;k<brojac;k++)
  57. {
  58. if(palindromi[k]<ci)
  59. {
  60. int ti=ci+palindromi[k];
  61. if(ti<=e && !visited[ti])
  62. {
  63. q.push(ti);
  64. visited[ti]=true;
  65. q.push(cekor+1);
  66. }
  67. }
  68. }
  69.  
  70. }
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement