题目的大意是给了你有限个棍子以及每个棍子的长度,而且所有的棍子都是由有限个长度相同的棍子截断得到的,让你求被截棍子的最小长度

搜索剪枝神题,做的我够呛

提供一个比较好的解题报告  http://www.cnblogs.com/mycapple/archive/2012/08/14/2638430.html

比较奇怪的是数组开小了WA了,开大就过了

代码还是比较挫的

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; int shu[];
int hash[];
int feng,n,ok,chu,fail;
int nhash[]; void dfs(int all,int add)
{
int i;
if(add==chu){
ok=;return ;
}
if(ok==||fail==){
return ;
}
if(all>&&hash[]==){ //第一根木棍没有被利用,就结束整个搜索
fail=;return;
} for(i=;i<=n;i++){
if(hash[i]==)continue; if(all==(add*feng)){
if(hash[i-]==){//当前的第一根木棍 组成一定要成功
return;
}
}
if((all+shu[i])<=((add+)*feng)){
if(shu[i-]==shu[i]&&hash[i-]==)continue;
hash[i]=;
if((all+shu[i])==((add+)*feng))
dfs(all+shu[i],add+);
else
dfs(all+shu[i],add);
hash[i]=;
}
}
} int cmp(int a,int b){
return a>b;
} int main()
{
int all,i,j;
while(scanf("%d",&n)!=EOF){
if(n==)return ;
int max=,temp;
all=; for(i=;i<=n;i++){
scanf("%d",&shu[i]);
all+=shu[i];
if(shu[i]>max)max=shu[i];
}
sort(&shu[],&shu[n+],cmp); for(i=max;i<=all;i++){
if(all%i!=)continue;
for(j=;j<=n;j++)hash[i]=;
hash[]=;
feng=i;
chu=all/i;
fail=;
ok=;
dfs(,);
if(ok==)break;
} printf("%d\n",feng);
} return ;
}

poj1011---DFS的更多相关文章

  1. poj1011 Sticks(DFS+剪枝)

    题目链接 http://poj.org/problem?id=1011 题意 输入n根棍子的长度,将这n根棍子组合成若干根长度相同的棍子,求组合后的棍子的最小长度.这题是poj2362的加强版,思路与 ...

  2. poj1011(DFS+剪枝)

    题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...

  3. POJ1011 (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 129606   Accepted: 30388 Descrip ...

  4. poj1011 Sticks(dfs+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110416   Accepted: 25331 Descrip ...

  5. poj1011 && uva307 DFS + 剪枝

    将木棒从大到小排列,保证每次的选择都是最长可选的木棒. 剪枝: 1 . 如果第 i 根木棒被选择却无法成功拼接,那么后面与其长度相同的也不能选择. 2 . 如果第 cnt + 1 根木棒无法成功拼接, ...

  6. poj1011 Sticks (dfs剪枝)

    [题目描述] George took sticks of the same length and cut them randomly until all parts became at most 50 ...

  7. dfs+剪枝 poj1011

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 113547   Accepted: 26078 问题描述 Ge ...

  8. POJ1011 木棒(dfs+剪枝)

    问题重述: Description乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始 ...

  9. 【poj1011】 Sticks

    http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...

  10. POJ1011

    今天搞了一下传说中的经典搜索题——poj1011,果然里面充斥着各种巧妙的剪枝,做完之后回味一下还是感觉构思太巧妙,所以总结记录一下加深理解. 原题:http://poj.org/problem?id ...

随机推荐

  1. ngnix进阶

    ngnix进阶 nginx: [warn] duplicate MIME type "text/html" in /usr/local/nginx/conf/nginx.conf: ...

  2. EF Code-First 学习之旅 EntityTypeConfiguration<TEntity>

    之前我们配置的实体都都在OnModelCreating方法中,如果有很多实体的话,OnModelCreating方法管理很麻烦 我们可以用单独的类来管理配置,继承EntityTypeConfigura ...

  3. java中Hashtable集合的常用方法

    实现Map集合的方法这里就不在讲了 https://www.cnblogs.com/xiaostudy/p/9510763.html public Object clone() 返回Hashtable ...

  4. JNI简单HelloWorld

    1.编写Java代码 建立hello目录,编写HelloWorld.java: class HelloWorld { public native void displayHelloWorld(); s ...

  5. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  6. ROC 曲线,以及AUC计算方式

    ROC曲线: roc曲线:接收者操作特征(receiveroperating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性. ROC曲线的横轴: 负正类率(false ...

  7. eclipse中修改工程的Android版本

    项目根目录下project.properties的记录项目中所需要的环境信息,比如Android的版本等 project.properties示例如下: [html] view plaincopy # ...

  8. New Concept English three (31)

    35w/m 45 True eccentrics never deliberately set out to draw attention to themselves. They disregard ...

  9. react 学习文章

    生命周期 学习笔记 一些坑 项目完成后总结 理解Immutable 是否要同构如何同构 react组件最佳实践 redux集合所有的state props来源, 页面所有状态 数据的唯一来源 reac ...

  10. WeChat on Web 部分功能模拟实现

    Flask from flask import Flask,request,render_template,session,jsonify import time import requests im ...