Teamwork Brings Profits! 搜索枚举

算法:

搜索枚举,注意FOR循环初始值。

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;

//搜索 
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;
     for( 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);
  }
}

posted on 2012-08-16 19:32  more think, more gains  阅读(162)  评论(0编辑  收藏  举报

导航