Advertisement
Josif_tepe

Untitled

Apr 18th, 2023
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <map>
  5. #include <cstring>
  6. #include <queue>
  7. #include <algorithm>
  8. //#include <bits/stdc++.h>
  9. using namespace std;
  10. const int maxn = 22;
  11. int n;
  12. char mat[maxn][maxn];
  13. int dp[1 << maxn];
  14. int bits_i[maxn];
  15. int rec(int bitmask) {
  16.     if(__builtin_popcount(bitmask) == n) {
  17.         return 0;
  18.     }
  19.     if(dp[bitmask] != -1) {
  20.         return dp[bitmask];
  21.     }
  22.     int res = 2e9;
  23.     for(int i = 0; i < n; i++) {
  24.         if(bitmask & (1 << i)) continue;
  25.         int switched = __builtin_popcount(bitmask & bits_i[i]);
  26.        
  27.         res = min(res, rec(bitmask | (1 << i)) + switched);
  28.     }
  29.     return dp[bitmask] = res;
  30. }
  31. string to_string_bit(int x) {
  32.     string s = "";
  33.     while(x > 0) {
  34.         s += (x % 2) + '0';
  35.         x /= 2;
  36.     }
  37.     reverse(s.begin(), s.end());
  38.     return s;
  39. }
  40. int main() {
  41.     ios_base::sync_with_stdio(false);
  42.     cin >> n;
  43.    
  44.     for(int i = 0; i < n; i++) {
  45.      
  46.         for(int j = 0; j < n; j++) {
  47.             cin >> mat[i][j];
  48.         }
  49.        
  50.         int num = 0;
  51.         for(int j = n - 1; j >= 0; j--) {
  52.             if(mat[i][j] == 'D') {
  53.                 num |= (1 << j);
  54.             }
  55.         }
  56.         bits_i[i] = num;
  57.     }
  58.    
  59.     fill(dp, dp + (1 << maxn), -1);
  60.     cout << rec(0) << endl;
  61.     return 0;
  62. }
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement