Codeforces Gym 101473D Folding Machine (暴力搜索)

题目连接:

http://codeforces.com/gym/101473/attachments

原先以为按照这个复杂度还要剪一下支,没想到暴力dfs完全可以通过。

代码:

#include <iostream>
#include <cstring>
using namespace std;
int a[51],b[51];
int n,m;
int aaa[51];

bool check(int i)       //就在于是理解题意
{                       
    for(i;i<=50;i++)    
        if(aaa[i]>0)
            return false;
    return true;
}
int folding(int flag)
{
    // int aaa[51];
    memset(aaa,0,sizeof(aaa));
    for(int i=0;i<=50;i++)
        aaa[i]=a[i];
    int theright=flag+1;
    while( !check(theright) )
    {
        aaa[flag]+=aaa[theright];
        aaa[theright]=0;
        theright++;
        flag--;
    }
    flag=0;
    while(aaa[flag]==0)flag++;
    int thenow=31;
    for(;flag<=50&&thenow<=50;flag++)
        a[thenow++]=aaa[flag];
    thenow=0;
    for(int i=31;i<=50;i++)
        if(a[i]!=0)thenow++;
    return thenow;
}
bool dfs(int nn)
{
    int thea[51];
    memset(thea,0,sizeof(thea));
    for(int i=1+30;i<=nn+30;i++)
        thea[i]=a[i];
    if(nn<=m)
    {
        if(nn==m)
        {
            for(int j=31;j<=50;j++)
                if(a[j]!=b[j])
                    return false;
            cout<<"S"<<endl;
            return true;
        }
        return false;
    }
    for(int i=1+30;i<nn+30;i++)     //这里在看一下
    {
        int thenn=folding(i);
        bool haha= dfs(thenn);
        if(haha)return true;
        for(int i=31;i<=nn+30;i++)
            a[i]=thea[i];
    }
    return false;
}

int main()      //如果是多个要注意每次都要初始化的。
{
    cin>>n;
    for(int i=1+30;i<=n+30;i++)
        cin>>a[i];
    cin>>m;
    for(int i=1+30;i<=m+30;i++)
        cin>>b[i];
    if(n==m)
    {
        if(a[31]==b[31])
        {
            for(int i=31;i<=n+30;i++)
                if(a[i]!=b[i])
                {
                    cout<<"N"<<endl;
                    return 0;
                }
            cout<<"S"<<endl;
            return 0;
        }
        else
        {
            int bao=n;
            for(int i=31;i<=n+30;i++)
                if(a[i]!=b[30+bao])
                {
                    cout<<"N"<<endl;
                    return 0;
                }
                else
                    bao--;
            cout<<"S"<<endl;           
            return 0;
        }
    }
    bool haha =dfs(n);
    if(!haha)cout<<"N"<<endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值