Monkey and Banana(HDU-1069)
第一次写博客,随便看看就好了
这题不会动态规划,我只会用递归哈哈
#include <bits/stdc++.h>
using namespace std;
int n,maxx=-1;
struct node{
int x,y,z;
};
node nodes[100];
bool judge(int a,int b,int a1,int b1){
if(min(a,b)<min(a1,b1)&&max(a,b)<max(a1,b1))return true;
return false;
}
int dfs(int x,int y,int z,int sum){//其实z多余了不用放入递归
for(int i=1;i<=n;i++){
int tox=nodes[i].x,toy=nodes[i].y,toz=nodes[i].z;
if(judge(tox,toy,x,y)){
dfs(tox,toy,toz,sum+toz);
}
if(judge(tox,toz,x,y)){
dfs(tox,toz,toy,sum+toy);
}
if(judge(toy,toz,x,y)){
dfs(toy,toz,tox,sum+tox);
}
}
maxx=max(sum,maxx);
}
int main(){
while(1){
maxx=-1;
cin>>n;
for(int i=1;i<=n;i++){
cin>>nodes[i].x>>nodes[i].y>>nodes[i].z;
}
dfs(11000,11000,11000,0);
cout<<maxx<<endl<<endl;
}
}
代码如上:
sum是每次递归到无法继续叠放时的最大高度
maxx是保存每次得出的sum的最大值,就是最后答案
dfs的形参 x y z意思现在最上面的砖块的长,宽,高。
for循环遍历所有的砖块:如果符合条件则将这块砖头叠上去(三条边任意两条都判定一下,是否符合条件),然后它的剩下的那条边就为高,加入sum。
只要for循环遍历都找不到符合条件的就是递归终点,随后更新maxx值就行了