Advertisement
wym1111

Untitled

Apr 15th, 2024
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5. int a[4];
  6. int ans = 200;
  7.  
  8. void dfs (int x, int b[4]) {
  9.     for (int i = 0; i < 4; i ++) {
  10.         cout << b[i] << ' ';
  11.     }
  12.     cout << x << endl;
  13.     if (x == 0) {
  14. //      b: 2 1 0 6
  15. //      20 16
  16.         int res = (b[0] + b[1]) * 10 + b[2] + b[3];
  17.         ans = min(ans, res);
  18.         return;
  19.     }
  20.     for (int i = 0; i <= 4; i ++) {
  21.         if(b[i] != -1) continue;
  22.         int c[4];
  23.         for (int j = 0; j < 4; j ++) c[j] = b[j];
  24.         c[i] = a[x - 1];
  25.         dfs(x - 1, c);
  26.     }
  27. }
  28.  
  29. int main() {
  30.     cin >> n;
  31.     for (int i = 0; i < 4; i ++) {
  32.         a[i] = n % 10;
  33.         n /= 10;
  34.     }
  35.     cout << "a: ";
  36.     for (int i = 0; i < 4; i ++) cout << a[i] << ' ';
  37.     cout << endl;
  38.     int s[4] = {-1, -1, -1, -1};
  39.     dfs(4, s);
  40.     cout << ans << endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement