好久没做算法题了,闲来无事,就把过去没ac的题拿出来做做,没看原来的代码,一次ac。现在看看原来的代码,发现思路太窄了。当时肯定做了很久,这东西有时候就得跳出来换个思路再来。
附上ac代码:
#include<iostream> #include<cstring> using namespace std; int n,m,ord,maxe=0,earning=0; int order[25][3]; void dfs(int trans[],int h){ // went through all orders and count total earnings if (h==ord){ earning = 0; for (int i=0;i<m;i++){ earning += trans[i]; } if (earning>maxe) maxe=earning; return; } // accept this order bool ok = true; int tmptrans[7]; memcpy(tmptrans,trans,sizeof(tmptrans)); for (int i=order[h][0];i<order[h][1];i++){ tmptrans[i] += order[h][2]; if (tmptrans[i]>n){ // this order cannot be accepted ok = false; break; } } if (ok) dfs(tmptrans,h+1); // reject order h dfs(trans,h+1); } int main(){ while(cin>>n>>m>>ord&&(n||m||ord)){ maxe=earning=0; memset(order,0,sizeof(order)); for (int i=0;i<ord;i++){ cin>>order[i][0]>>order[i][1]>>order[i][2]; } int t[7]; memset(t,0,sizeof(t)); dfs(t,0); cout<<maxe<<endl; } }
时间: 2024-10-05 20:39:04