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