搜索与剪枝

暴力解决实际问题往往是最为行之有效的,在搜索中显得尤为明显。介于个人水平低微,就不详细说明。转载一篇dalao的博文《浅谈搜索剪枝》,讲的比较详细。

下面给出一些实际问题,以供参考。

51nod 1060

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#define ll long long
using namespace std;
const int maxn=16;
int prime[maxn]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
ll ans,num;
ll n;
void dfs(ll ANS,ll NUM,int k,int p)
{
    if(k>maxn) return;
    if(NUM>num)
    {
        num=NUM;
        ans=ANS;
    }
    if(NUM==num&&ANS<ans) ans=ANS;
    for(int i=1;i<=p;i++)
    {
        if(ANS<=n/prime[k])
        {
            ANS=ANS*prime[k];
            dfs(ANS,NUM*(i+1),k+1,i);
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        ans=1,num=1;
        dfs(1,1,0,65);
        printf("%lld %lld\n",ans,num);
    }
    return 0;
}


2018年全国多校算法寒假训练营练习比赛(第二场)
C 小仙女过生日啦  
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<vector>
#include<cstring>
#include<cmath>

using namespace std;
#define inf  0x3f3f3f3f
const int maxn = 2e2+9;
const double eps = 1e-2; 
typedef long long ll;
 
struct node{
    double x,y;
}p[maxn];
 
inline double S(node a,node b,node c)
{
    return fabs((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y))/2; 
}
int n;
double dp[maxn][maxn];
 
inline int check(int x,int y,int z)
{
    double s = S(p[x],p[y],p[z]);
    for(int i=1;i<=n;i++)
    {
        if(i==x||i==y||i==z) 
            continue;
        double tmp=S(p[x],p[y],p[i])+S(p[x],p[z],p[i])+S(p[y],p[z],p[i]);
        if(fabs(tmp-s)<=eps)
            return 0; 
    }
    return 1;
} 
 
inline double dfs(int x,int y){
    if(x>=y-1) 
        return 0;
    if(x==y-2) 
        return S(p[x],p[x+1],p[x+2]);
    if(dp[x][y]!=-1) 
        return dp[x][y];
     
    double res=inf;
    for(int i=x+1;i<y;i++)
    {
        if(check(x,y,i)==0) 
            continue; 
        double tmp = max(S(p[x],p[y],p[i]),max(dfs(x,i),dfs(i,y)));
        res = min(res,tmp);
    }
    dp[x][y] = res;
    return res;
}
 
int main(){
    double a = 2e9+1;
    while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%lf%lf",&p[i].x,&p[i].y);
            }
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++) 
                    dp[i][j]=-1.0;
            printf("%.1f\n",dfs(1,n));  
        }
    return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值