题意:输入n 输入n行n列 从n行中找出n个数 保证任意两个都不在同一列 求这些数的和。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 17;
int a[maxn][maxn];
int dp[17][(1<<16)];
int cal(int x)
{int len = 0;while(x){if(x & 1)len++;x >>= 1;}return len;
}
int main()
{int T, n;scanf("%d", &T);for(int kase = 1; kase <= T; kase++){scanf("%d", &n);for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)scanf("%d", &a[i][j]);memset(dp, 0, sizeof(dp));for(int i = 1; i <= n; i++)for(int j = 0; j <= (1<<n) - 1; j++){if(cal(j) != i-1) continue;for(int k = 0; k < n; k++){if((j&(1<<k)) == 0){dp[i][j^(1<<k)] = max(dp[i][j^(1<<k)], dp[i-1][j] + a[i][n-k]);}}}printf("Case %d: %d\n", kase, dp[n][(1<<n)-1]);}return 0;
}