题目感觉是图的题。。但是做的是POJ简单搜索的专题,嗯,没有思路。。看了大神的代码才学会的,回溯+dfs。。
等以后图论学好了,在回头用图论做,我在网上看到有大神用最大割来做,加星星了,等回头在用别的方法来做,先贴上DFS回溯的代码,后面学会最大割再用最大割的方法来A。最近心态爆炸,不知道为啥,总感觉难道有1说1,有2说2的性格不好嘛?不喜欢空口说大话表面功夫。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=22;
int data[maxn][maxn];
bool book[maxn];
int n;
int ans=0;
void init()
{
cin>>n;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
cin>>data[i][j];
memset(book,0,sizeof(book));
}
void dfs(int local,int sum)
{
book[local]=1;
int temp=sum;
for(int i=1;i<=n;i++)
if(book[i])
temp=temp-data[i][local];
else
temp=temp+data[i][local];
ans=temp>ans?temp:ans;
if(temp>sum)
for(int i=local+1;i<=n;++i)
dfs(i,temp);
book[local]=0;
}
int main()
{
init();
dfs(1,0);
cout<<ans<<endl;
return 0;
}