POJ 1011 Sticks (backtrakcing+剪枝)
来源:互联网 发布:黑马程序员讲师 编辑:程序博客网 时间:2024/05/17 02:55
POJ 1011 Sticks backtrakcing+剪枝
#include <iostream>#include <vector>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;int n,sum,maxx,marked,visited[100],ans;vector <int> v;void initial(){v.clear();sum=0;maxx=0;marked=0;for(int i=0;i<100;i++) visited[i]=-1;}bool cmp(int a,int b){return a>b;} void input(){int x;for(int i=0;i<n;i++){scanf("%d",&x);v.push_back(x);sum+=x;maxx=max(maxx,x);}sort(v.begin(),v.end(),cmp);}bool dfs(int cnt,int x,int pos){if(cnt==0) return true;else if(x==0) return dfs(cnt-1,ans,-1);else{for(int i=pos+1;i<v.size();i++){if(x>=v[i] && visited[i]!=marked){visited[i]=marked;if( dfs(cnt,x-v[i],i) ) return true;else{visited[i]=marked-1;if( v[i]==x || x==ans) return false;//剪枝的核心} }}return false;}}void computing(){for(int i=maxx;i<=sum/2;i++){if(sum%i==0){marked++;ans=i;if(dfs(sum/i,i,-1)){cout<<ans<<endl;return;}}}cout<<sum<<endl; }int main(){while(scanf("%d",&n)!=EOF && n){initial();input();computing();}return 0;}
- POJ 1011 Sticks (backtrakcing+剪枝)
- Sticks poj 1011剪枝
- poj 1011Sticks(DFS +剪枝)
- [深搜+剪枝] POJ - 1011 Sticks
- POJ 1011 Sticks dfs + 剪枝
- POJ 1011 Sticks DFS+剪枝
- poj 1011 Sticks -----dfs+剪枝
- POJ 1011 Sticks (DFS + 剪枝)
- POJ 1011 Sticks DFS 剪枝
- POJ 1011 Sticks 深搜+剪枝
- POJ 1011 Sticks 【深搜+剪枝】
- poj 1011 Sticks 【DFS】+【剪枝】
- [POJ 1011]Sticks(DFS剪枝)
- poj 1011 Sticks(搜索+剪枝)
- poj 1011 :Sticks (dfs+剪枝)
- POJ 1011 Sticks dfs+剪枝
- poj 1011 Sticks(dfs+剪枝)
- POJ 1011 Sticks DFS+剪枝
- HDOJ 3469 - Treasure Hunting BFS+二分图最大匹配...深入理解二分图最大匹配..
- Hadoop学习之莎士比亚文档词频统计
- (笔记)C++ Primer/第四章 数组和指针
- Linux系统下超强远程同步备份工具Rsync使用详解
- HDU 3313 Key Vertex 求割点数(好题)
- POJ 1011 Sticks (backtrakcing+剪枝)
- ENUM
- POJ3690+位运算
- 编程之美3.3计算字符串的相似度
- unity3d Shader开发简介
- fopen
- Object-C学习(八):文件管理【一】
- Linux_make命令提示no found make的解决办法
- 整理用Java实现数字转化成字符串左边自动补零方法