POJ 2531
直接暴力的
#include <stdio.h> #include <string.h> #define maxn 21 int matrix[maxn][maxn]; int sel[maxn]; int n; int max; int tot; void select(int cur) { if(cur==n) { if(tot==n||tot==0) return; int i,j; int amount=0; for(i=0;i<n;i++) { if(!sel[i]) continue; for(j=0;j<n;j++) { if(sel[j]) continue; amount+=matrix[i][j]; } } if(amount>max) max=amount; return; } sel[cur]=1;//选 tot++; select(cur+1); tot--; sel[cur]=0;//不选 select(cur+1); } int main() { while(scanf("%d",&n)!=EOF) { tot=0; memset(sel,0,sizeof(sel)); int i,j; max=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&matrix[i][j]); } } sel[0]=1; select(0); printf("%d\n",max); } return 0; }