poj 1011 DFS
来源:互联网 发布:上海运管处网络平台 编辑:程序博客网 时间:2024/05/16 05:15
一道恶心的搜索题,主要是要考虑很多剪枝的情况,不然就只有TLE了。。。。
题意:给n个木棍,每个木棍有一个长度,要求给木棍分组,使得分组后,每个组的木棍长度之和相等,且和要最小。输出这个最小的长度之和。
分析:既然是要使分组后木棍长度之和最小看,能想到的是从最小的长度之和开始枚举,判断这个和是否合法,合法则是所求解,否则继续枚举下一个长度和。当时这个过程中有许多是需要剪枝的地方,如下:
1:枚举的长度和必然是所有木棍长度和的因子;
2:给枚举过的木棍作标记,以便不重复枚举;
3:将木棍从大到小枚举,如果某个木棍非法,则枚举下一个;
4:记录上一个木棍的长度,若其非法,则不再枚举相同长度的木棍;
#include <cstdio>#include <stack>#include <set>#include <iostream>#include <string>#include <vector>#include <queue>#include <list>#include <functional>#include <cstring>#include <algorithm>#include <cctype>#include <string>#include <map>#include <iomanip>#include <cmath>#define LL long long#define ULL unsigned long long#define SZ(x) (int)x.size()#define Lowbit(x) ((x) & (-x))#define MP(a, b) make_pair(a, b)#define MS(arr, num) memset(arr, num, sizeof(arr))#define PB push_back#define F first#define S second#define ROP freopen("input.txt", "r", stdin);#define MID(a, b) (a + ((b - a) >> 1))#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define lrt rt << 1#define rrt rt << 1|1#define root 1,m,1#define BitCount(x) __builtin_popcount(x)#define BitCountll(x) __builtin_popcountll(x)#define LeftPos(x) 32 - __builtin_clz(x) - 1#define LeftPosll(x) 64 - __builtin_clzll(x) - 1const double PI = acos(-1.0);const LL INF = (((LL)1)<<62)+1;using namespace std;const double eps = 1e-5;const int MAXN = 300 + 10;const int MOD = 1000007;const double M=1e-8;const int N=100;typedef pair<int, int> pii;typedef pair<int, string> pis;const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};int n,m, a[N],cnt[N];bool vis[N];bool cmp(int a,int b){ return a>b;}//bool dfs(int k,int len,int sum)//{// int i,j;// if (!k) return true;// for (i=0;i<n;i++) if (cnt[a[i]]) { cout<<"->"<<k<<" "<<len<<" "<<sum<<endl;// int t=sum+a[i];// if (t>len) break;// cnt[a[i]]--;// if (t==len) {// if (dfs(k-1,len,0)) return true;// }// else if (dfs(k,len,t)) return true;// cnt[a[i]]++;// }// return false;//}int len;bool dfs(int k,int sum,int cnt) // 枚举到第k个木棍,此时长度和为sum,还需要分cnt组{ int i,j; if (!cnt) return true; // 木棍全部分组完毕 else if (sum==len) return dfs(0,0,cnt-1); // 分完一组,从第一个木棍开始继续分组 else { int pre=0; for (i=k;i<n;i++) if (!vis[i]) { // 没有使用过该木棍 int t=sum+a[i]; if (t>len || pre==a[i]) continue; // 既没超过枚举的每组长度和,也不和上一个木棍长度相等_, vis[i]=true; pre=a[i]; if (dfs(i+1,t,cnt)) return true; // 枚举下一个木棍 vis[i]=false; if (k==0) return false; // 若第一个木棍与其它所有木棍都无法凑齐当前枚举的每组长度和,则说明这个长度和非法。这个剪枝很重要!!!! } return false; }}int main(){ int i,j; while(~scanf("%d",&n),n) { int sum=0; for (i=0;i<n;i++){ scanf("%d",a+i); sum+=a[i]; } sort(a,a+n,cmp); for (i=a[0];i<=sum;i++) if (sum%i==0) { // 每个组的木棍长度和是总长度的因子 int cnt=sum/i; len=i; MS(vis,false); if (dfs(0,0,cnt)) { printf("%d\n",i); break; } } }}/*附两组数据:6440 40 30 35 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40 40 25 39 46 40 10 4 40 40 37 18 17 16 15 40 40 40 40 40 40 40 4045445 15 3 2 4 11 1 8 8 8 15 3 2 4 11 1 8 8 8 15 3 2 4 11 1 8 8 8 15 3 2 4 11 1 8 8 8 15 3 2 4 11 1 8 8 820*/
0 0
- poj 1011 dfs
- [POJ-1011] DFS
- poj 1011 Sticks【dfs】
- POJ 1011 (DFS+剪枝)
- poj 1011 Sticks (DFS)
- poj 1011Sticks dfs
- POJ 1011 Sticks DFS
- poj 1011 dfs剪枝
- poj 1011 DFS
- DFS POJ 1011 Sticks
- POJ 1011 Sticks【dfs】
- POJ 1011 Sticks dfs
- POJ 1011 (DFS)
- POJ 1011Sticks【DFS】
- poj 1011Sticks(DFS +剪枝)
- poj 1011 Sticks(dfs)
- POJ 1011 Sticks dfs + 剪枝
- DFS 剪枝1 poj 1011
- share_ptr的正确使用方法
- 7-13 new,局部对象,MFC全局对象
- [转]深入分析几种PHP获取客户端IP的情况
- Android基本对话框
- 2012年5月SAT香港真题解析
- poj 1011 DFS
- AWK中,NR和FNR的区别
- 虚指针存在证明及虚函数表
- Android中dp、dip、px、sp之间的区别
- SIM显示字 SPN,PLMN ,MCC,MNC
- asm下重定向数据文件小例(使用toad工具)
- leetcode-二分搜索:Search Insert Position
- 视频监控系统:C/S & B/S
- ExtJS5.0的mvvm分层思想