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 N, m;
- cin >> N >> m;
- vector<bool> zabraneti(10);
- for(int i = 0; i < m; i++) {
- int x;
- cin >> x;
- zabraneti[x] = true;
- }
- queue<int> q;
- vector<bool> visited(2000005, false);
- for(int i = 100; i <= 100000; i++) {
- int mozhe = 1;
- int broj_na_cifri = 0;
- for(int j = i; j > 0; j /= 10) {
- int cifra = j % 10;
- broj_na_cifri++;
- if(zabraneti[cifra]) {
- mozhe = 0;
- }
- }
- if(mozhe == 1) {
- q.push(i);
- q.push(broj_na_cifri);
- visited[i] = true;
- }
- }
- while(!q.empty()) {
- int broj = q.front();
- q.pop();
- int stiskanja = q.front();
- q.pop();
- if(broj == N) {
- cout << min(stiskanja, abs(N - 100)) << endl;
- return 0;
- }
- if(broj + 1 < 200000 and !visited[broj + 1]) {
- q.push(broj + 1);
- q.push(stiskanja + 1);
- visited[broj + 1] = true;
- }
- if(broj > 0 and !visited[broj - 1]) {
- q.push(broj - 1);
- q.push(stiskanja + 1);
- visited[broj - 1] = true;
- }
- }
- cout << abs(N - 100) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement