1 #include <stdio.h> 2 #include <iostream> 3 #define MAXN 21 4 using namespace std; 5 6 int _m[MAXN][MAXN]; 7 int flag[MAXN]; 8 9 int n; 10 int index; 11 int _max; 12 int num; 13 void DFS(int time); 14 int main() 15 { 16 //freopen("acm.acm","r",stdin); 17 //freopen("out.acm","w",stdout); 18 int i; 19 int j; 20 memset(flag,0,sizeof(flag)); 21 cin>>n; 22 _max = -1; 23 index = 1; 24 for(i = 0; i < n; ++ i) 25 { 26 for(j = 0; j < n; ++ j) 27 { 28 scanf("%d",&_m[i][j]); 29 } 30 } 31 for(index = 1; index <= n/2+1; ++ index) 32 { 33 num = 0; 34 DFS(0); 35 } 36 cout<<_max<<endl; 37 return 0; 38 } 39 40 void DFS(int time) 41 { 42 if(time == n) 43 return; 44 if(num == index) 45 { 46 int i; 47 int j; 48 int sum = 0; 49 for(i = 0; i < n; ++ i) 50 { 51 for(j = i+1;j < n; ++ j) 52 { 53 if(flag[i] != flag[j]) 54 { 55 sum += _m[i][j]; 56 } 57 } 58 } 59 if(sum > _max) 60 { 61 _max = sum; 62 } 63 return; 64 } 65 flag[time] = 1; 66 ++ num; 67 DFS(time + 1); 68 -- num; 69 flag[time] = 0; 70 DFS(time + 1); 71 }