Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- char mat[22][22];
- short n;
- short dp[1 << 21];
- vector<int> masks;
- 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;
- res = min(res, rec(bitmask | (1 << i)) + __builtin_popcount(bitmask & masks[i]));
- }
- return dp[bitmask] = res;
- }
- 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, pow = n - 1;
- for(int j = n - 1; j >= 0; j--) {
- if(mat[i][j] == 'D') {
- num |= (1 << pow);
- }
- pow--;
- }
- masks.push_back(num);
- }
- memset(dp, -1, sizeof dp);
- cout << rec(0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement