深搜剪枝
来源:互联网 发布:字幕软件哪个好 编辑:程序博客网 时间:2024/05/17 23:35
#include<bits/stdc++.h>//思路暴力枚举每个位置放的个数using namespace std;int c[12];int p[12];int n,a,b;int Ans;int ans[12];//保存结果用int s1,s2;void dfs(int pos,int num){ if(num>=Ans) return;//普遍剪枝 if(pos==n) { int i; for(i=1;i<=n;i++) if(c[i]>=0) {break;} if(i>n){Ans=num;} return; } int st=0; if(pos==2)//按题目要求剪枝,没2-n范围的要求要tl { int s=0,num=0; while(1) { s=s+b; num++; if(s>s1) break; } st=num; } else if(pos==n-1) { int s=0,num=0; while(1) { s=s+b; num++; if(s>s2) break; } st=num; } for(int i=st;i<=p[pos];i++) { if(i==0) {ans[pos]=0;dfs(pos+1,num);}//debug else { if(c[pos-1]<0&&c[pos]<0&&c[pos+1]<0) continue; int tmp1=c[pos-1],tmp2=c[pos],tmp3=c[pos+1];//回溯易出bug c[pos-1]=c[pos-1]-i*b; //pos-debug c[pos]=c[pos]-i*a; c[pos+1]=c[pos+1]-i*b; ans[pos]=i; dfs(pos+1,num+i); c[pos-1]=tmp1; c[pos]=tmp2; c[pos+1]=tmp3; } }}int main(){ while(scanf("%d%d%d",&n,&a,&b)!=EOF) { for(int i=1;i<=n;i++) {scanf("%d",&c[i]);} s1=c[1],s2=c[n]; for(int i=2;i<=n-1;i++) //预处理每个位置放的个数的最大值 { int j; for(j=1;;j++) { if(b*j>c[i-1]&&a*j>c[i]&&b*j>c[i+1]) break; } p[i]=j; } Ans=999999999; dfs(2,0); cout<<Ans<<endl; } return 0;}
阅读全文
0 0
- sticks(深搜剪枝)
- hdu1010 深搜剪枝.
- hdu1455Sticks【深搜剪枝】
- 深搜的剪枝
- 深搜剪枝
- 简单深搜剪枝_Poj_1088
- Zipper 深搜与剪枝
- HDU 1010 经典深搜+奇偶剪枝
- HDU 1010 经典深搜+奇偶剪枝
- poj 2362 Square(简单深搜+剪枝)
- hdu1010Tempter of the Bone深搜剪枝
- USACO ORZ(hdu4277 深搜剪枝)
- POJ 3172 Scales (剪枝深搜)
- poj 1040 Transportation 深搜剪枝
- poj3278(广搜剪枝)
- poj1465剪枝广搜
- hdu4876 暴搜剪枝
- 剪枝
- 【数据结构与算法】【简介】数据结构到底是什么
- Elasticsearch简单操作(Java代码)
- 交换机和VLAN
- 递归与非递归遍历二叉树(各三种)
- 两次快速点击导致的问题
- 深搜剪枝
- Linux专辑
- Banner
- 《数据结构》学习笔记(3)
- Spring Boot学习
- codeforces 401A Vanya and Cards
- dfs序+时间戳
- app后端设计--总目录
- ROS学习第五弹(发布和订阅 Python写 Publisher and Subscriber)