Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <fstream>
- #include <array>
- using namespace std;
- int main()
- {
- vector<int> palindromes;
- for(int i = 1; i <= 99999; i++) {
- vector<int> v;
- int n = i;
- while(n > 0) {
- int digit = n % 10;
- v.push_back(digit);
- n /= 10;
- }
- int l = 0, r = (int) v.size() - 1;
- bool ok = true;
- while(l < r) {
- if(v[l] == v[r]) {
- l++;
- r--;
- }
- else {
- ok = false;
- break;
- }
- }
- if(ok) {
- palindromes.push_back(i);
- }
- }
- int S, E;
- cin >> S >> E;
- queue<int> Q;
- Q.push(S);
- Q.push(0);
- vector<bool> visited(100000, false);
- while(!Q.empty()) {
- int number = Q.front();
- Q.pop();
- int cekor = Q.front();
- Q.pop();
- if(number == E) {
- cout << cekor << endl;
- return 0;
- }
- for(int i = 0; i < palindromes.size(); i++) {
- if(palindromes[i] < number and palindromes[i] + number <= E and !visited[palindromes[i] + number]) {
- Q.push(palindromes[i] + number);
- Q.push(cekor + 1);
- visited[palindromes[i] + number] = true;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement