Advertisement
Josif_tepe

Untitled

Mar 14th, 2023
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. char mat[22][22];
  5. int n;
  6. int dp[1 << 21];
  7.  
  8. int rec(int bitmask) {
  9.     if(__builtin_popcount(bitmask) == n ) {
  10.          
  11.         return 0;
  12.     }
  13.     if(dp[bitmask] != -1) {
  14.         return dp[bitmask];
  15.     }
  16.     int res = 2e9;
  17.     for(int i = 0; i < n; i++) {
  18.         if(bitmask & (1 << i)) continue;
  19.         int c = 0;
  20.         for(int j = 0; j < n; j++) {
  21.             if(bitmask & (1 << j)) {
  22.                 if(mat[i][j] == 'D') {
  23.                     c++;
  24.                 }
  25.             }
  26.         }
  27.         res = min(res, rec(bitmask | (1 << i)) + c);
  28.     }
  29.     return dp[bitmask] = res;
  30. }
  31. int main() {
  32.     ios_base::sync_with_stdio(false);
  33.     cin >> n;
  34.     for(int i = 0; i < n; i++) {
  35.         for(int j = 0; j < n; j++) {
  36.             cin >> mat[i][j];
  37.         }
  38.     }
  39.     memset(dp, -1, sizeof dp);
  40.     cout << rec(0) << endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement