本来已经按自己的回溯做出来了,下面这个是老师的做法,感觉老师的思路不对,求大佬帮我改改
#include<bits/stdc++.h>
using namespace std;
int t[5],a[65],ans,sum,now,y,maxtime,maxdeep;
void dfs(int x)
{
if(x>maxdeep) {
maxtime=max(maxtime,sum-now);
cout<<"now:"<<now<<endl;
return ;
}
if(now+a[x]<=sum/2) {
now+=a[x];
cout<<"now+:"<<now<<endl;
dfs(x+1);
now-=a[x];
}
dfs(x+1);
}
int main()
{
for(int i=1;i<=4;i++) {
cin >> t[i];
}
for(int i=1;i<=4;i++)
{
memset(a,0,sizeof(a));
sum = 0;now = 0;
for(int j=1;j<=t[i];j++)
{
cin >> a[j];
sum += a[j];
}
maxdeep = t[i];
maxtime = 0;
dfs(1);
ans += maxtime;
cout << "maxtime:" << maxtime <<" now:"<<now<<endl;
}
cout << ans;
return 0;
}