Algorithm:
Search for enumeration. Pay attention to the initial values of the for loop.
View code
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <iostream> # include <vector> # include <string> # include <math. h> # include <map> # include <set> # include <algorithm> using namespace STD; int MP [20] [20]; int hash [20]; int maxn; int N; // search void DFS (int x, int num, int sum) {If (num = n/2) {If (sum> maxn) maxn = sum; return;} For (INT I = x + 1; I <= N; I ++) {If (hash [I]) continue; hash [I] = 1; fo R (Int J = I + 1; j <= N; j ++) {if (I = J) continue; If (! Hash [J]) {// hash [I] = 1; hash [J] = 1; DFS (x + 1, num + 1, sum + MP [I] [J]); // hash [I] = 0; hash [J] = 0 ;}} hash [I] = 0 ;}} int main () {While (scanf ("% d", & N), n) {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) scanf ("% d", & MP [I] [J]); maxn = 0; memset (hash, 0, sizeof (hash); DFS (0, 0, 0); printf ("% d \ n", maxn );}}