Advertisement
Josif_tepe

Untitled

Jan 26th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5. int main()
  6. {
  7.     int N, m;
  8.     cin >> N >> m;
  9.      
  10.     vector<bool> zabraneti(10);
  11.     for(int i = 0; i < m; i++) {
  12.         int x;
  13.         cin >> x;
  14.         zabraneti[x] = true;
  15.     }
  16.     queue<int> q;
  17.     vector<bool> visited(2000005, false);
  18.      
  19.     for(int i = 100; i <= 100000; i++) {
  20.         int mozhe = 1;
  21.         int broj_na_cifri = 0;
  22.          
  23.         for(int j = i; j > 0; j /= 10) {
  24.             int cifra = j % 10;
  25.             broj_na_cifri++;
  26.             if(zabraneti[cifra]) {
  27.                 mozhe = 0;
  28.             }
  29.         }
  30.         if(mozhe == 1) {
  31.             q.push(i);
  32.             q.push(broj_na_cifri);
  33.             visited[i] = true;
  34.         }
  35.     }
  36.      
  37.     while(!q.empty()) {
  38.         int broj = q.front();
  39.         q.pop();
  40.         int stiskanja = q.front();
  41.         q.pop();
  42.          
  43.         if(broj == N) {
  44.             cout << min(stiskanja, abs(N - 100)) << endl;
  45.             return 0;
  46.         }
  47.         if(broj + 1 < 200000 and !visited[broj + 1]) {
  48.             q.push(broj + 1);
  49.             q.push(stiskanja + 1);
  50.             visited[broj + 1] = true;
  51.         }
  52.         if(broj > 0 and !visited[broj - 1]) {
  53.             q.push(broj - 1);
  54.             q.push(stiskanja + 1);
  55.             visited[broj - 1] = true;
  56.         }
  57.     }
  58.     cout << abs(N - 100) << endl;
  59.      
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement