Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <map>
- #include <cstring>
- #include <queue>
- #include <algorithm>
- //#include <bits/stdc++.h>
- using namespace std;
- const int maxn = 22;
- int n;
- char mat[maxn][maxn];
- int dp[1 << maxn];
- int bits_i[maxn];
- int rec(int bitmask) {
- if(__builtin_popcount(bitmask) == n) {
- return 0;
- }
- if(dp[bitmask] != -1) {
- return dp[bitmask];
- }
- int res = 2e9;
- for(int i = 0; i < n; i++) {
- if(bitmask & (1 << i)) continue;
- int switched = __builtin_popcount(bitmask & bits_i[i]);
- res = min(res, rec(bitmask | (1 << i)) + switched);
- }
- return dp[bitmask] = res;
- }
- string to_string_bit(int x) {
- string s = "";
- while(x > 0) {
- s += (x % 2) + '0';
- x /= 2;
- }
- reverse(s.begin(), s.end());
- return s;
- }
- 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 num = 0;
- for(int j = n - 1; j >= 0; j--) {
- if(mat[i][j] == 'D') {
- num |= (1 << j);
- }
- }
- bits_i[i] = num;
- }
- fill(dp, dp + (1 << maxn), -1);
- cout << rec(0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement