Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cstring>
- #include <algorithm>
- #include <fstream>
- using namespace std;
- int n;
- char mat[22][22];
- int dp[1 << 21];
- int bitmask_mat[22];
- int rec(int bitmask) {
- if(__builtin_popcount(bitmask) == n) {
- return 0;
- }
- if(dp[bitmask] != -1) {
- return dp[bitmask];
- }
- int result = 2e9;
- for(int i = 0; i < n; i++) {
- if(bitmask & (1 << i)){
- continue;
- }
- int changes = __builtin_popcount(bitmask & bitmask_mat[i]);
- result = min(result, rec(bitmask | (1 << i)) + changes);
- }
- return dp[bitmask] = result;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < n; j++) {
- cin >> mat[i][j];
- }
- int number = 0;
- for(int j = n - 1; j >= 0; j--) {
- if(mat[i][j] == 'D'){
- number |= (1 << j);
- }
- }
- bitmask_mat[i] = number;
- }
- memset(dp, -1, sizeof dp);
- cout << rec(0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement