Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <vector>
- using namespace std;
- bool is_palindrome(int x) {
- string s = "";
- while(x > 0) {
- int digit = x % 10;
- s += (digit + '0');
- x /= 10;
- }
- int i = 0, j = s.size() - 1;
- while(i < j) {
- if(s[i] != s[j]) {
- return false;
- }
- i++;
- j--;
- }
- return true;
- }
- int main() {
- vector<int> palindromes;
- for(int i = 1; i <= 100000; i++) {
- if(is_palindrome(i)) {
- palindromes.push_back(i);
- }
- }
- int S, E;
- cin >> S >> E;
- vector<bool> visited(100000, false);
- visited[S] = true;
- queue<int> q;
- q.push(S);
- q.push(0);
- while(!q.empty()) {
- int number = q.front();
- q.pop();
- int dist = q.front();
- q.pop();
- if(number == E) {
- cout << dist << endl;
- return 0;
- }
- for(int i = 0; i < palindromes.size(); i++) {
- int palindrome = palindromes[i];
- if(palindrome < number and palindrome + number <= E) {
- if(!visited[palindrome + number]) {
- visited[palindrome + number] = true;
- q.push(palindrome + number);
- q.push(dist + 1);
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement