Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- int main() {
- int p, n;
- cin >> p >> n;
- queue<int> q;
- vector<bool> visited(14, false);
- for(int i = 0; i < n; i++) {
- int x;
- cin >> x;
- visited[x] = true;
- }
- vector<bool> vis(100000, false);
- q.push(100);
- q.push(0);
- vis[100] = true;
- for(int i = 0; i < 99999; i++) {
- bool moze = true;
- int x = i;
- int cnt = 0;
- while(x > 0) {
- int digit = x % 10;
- if(visited[digit]) {
- moze = false;
- break;
- }
- cnt++;
- x /= 10;
- }
- if(moze and i != 100) {
- q.push(i);
- q.push(cnt);
- vis[i] = true;
- }
- }
- while(!q.empty()) {
- int number = q.front();
- q.pop();
- int dist = q.front();
- q.pop();
- if(number == p) {
- cout << min(dist, p - 100) << endl;
- return 0;
- }
- if(number + 1 < 100000 and !vis[number + 1]) {
- q.push(number + 1);
- q.push(dist + 1);
- vis[number + 1] = true;
- }
- if(number - 1 >= 0 and !vis[number - 1]) {
- q.push(number - 1);
- q.push(dist + 1);
- vis[number - 1] = true;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement