这一题就是标准的dfs就最值的方法
从每一个口开始遍历
跳进去
然后扫描周围,可以就进去,并且标记
知道尽头退出来,并回溯记录答案
在跳进去的洞穴继续扫描
#include <bits/stdc++.h>
using namespace std;
int n;
struct node
{
int a;//地雷数
bool b[25];//连接情况
}mp[25];//地窖数
int ans;
int ansc[25],c[25];
void dfs(int x,int u,int sum)//搜索部分
{
c[u] = x;
if(sum > ans)//记录部分
{
ans = sum;
int i = 1;
while(ansc[i] != 0)
{
ansc[i++] = 0;
}
for(int j = 1; j <= u; j++)
ansc[j] = c[j];
}
for(int i = x+1; i <= n; i++)
{
if(mp[x].b[i] == true)
{
mp[x].b[i] = false;
dfs(i,u+1,sum + mp[i].a);
mp[x].b[i] = true;
}
}
}
int main()
{
ios::sync_with_stdio(false);//加速cin,cout速度
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> mp[i].a;
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
cin >> mp[i].b[j];
for(int i = 1; i <= n; i++)
dfs(i,1,mp[i].a);
int i = 1;
while(ansc[i] != 0)
{
cout << ansc[i++] << " ";
}
cout << endl << ans;
return 0;
}