洛谷 P2089:烤鸡 ← dfs / bfs / 枚举法

【题目来源】
https://www.luogu.com.cn/problem/P2089

【题目描述】
猪猪 Hanke 得到了一只鸡。猪猪 Hanke 特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)。Hanke 吃鸡很特别,为什么特别呢?因为他有 10 种配料(芥末、孜然等),
每种配料可以放 1 到 3 克,任意烤鸡的美味程度为所有配料质量之和。
现在, Hanke 想要知道,如果给你一个美味程度 n ,请输出这 10 种配料的所有搭配方案。

【输入格式】
一个正整数 n,表示美味程度。

【输出格式】
第一行,方案总数。
第二行至结束,10 个数,表示每种配料所放的质量,按字典序排列。
如果没有符合要求的方法,就只要在第一行输出一个 0。

【输入样例】
11

【输出样例】
10
1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 2 1
1 1 1 1 1 1 1 2 1 1
1 1 1 1 1 1 2 1 1 1
1 1 1 1 1 2 1 1 1 1
1 1 1 1 2 1 1 1 1 1
1 1 1 2 1 1 1 1 1 1
1 1 2 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1

【说明/提示】
对于 100% 的数据,n≤5000。

【算法分析】
● 算法竞赛中常以‌ 1 秒内完成 1 亿次(1e8)基本操作‌为参考标准。
● DFS 是回溯法的核心实现方式,通过深度优先遍历解空间生成所有可能的方案。

【算法代码:
dfs

#include<bits/stdc++.h>
using namespace std;

int res[10000][10]; //3^10=59049 solutions
int path[10];
int cnt;

void dfs(int step,int remain) {
    if(step==10) {
        if(remain==0) {
            for(int i=0; i<10; i++) {
                res[cnt][i]=path[i];
            }
            cnt++;
        }
        return;
    }

    for(int i=1; i<=3; i++) {
        if(remain-i>=0) {
            path[step]=i;
            dfs(step+1,remain-i);
        }
    }
}

int main() {
    int n;
    cin>>n;
    if(n<10 || n>30) {
        cout<<0;
        return 0;
    }
    dfs(0,n);
    cout<<cnt<<endl;
    for(int i=0; i<cnt; i++) {
        for(int j=0; j<10; j++) {
            cout<<res[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

【算法代码二:bfs

#include<bits/stdc++.h>
using namespace std;

struct Node {
    int step;
    int remain;
    int path[10];
};

int res[10000][10]; //3^10=59049 solutions
int cnt;

void bfs(int n) {
    queue<Node> q;
    Node start= {0,n,{0}};
    q.push(start);

    while(!q.empty()) {
        Node cur=q.front();
        q.pop();
        if(cur.step==10) {
            if(cur.remain==0) {
                for(int i=0; i<10; i++) {
                    res[cnt][i]=cur.path[i];
                }
                cnt++;
            }
            continue;
        }

        for(int i=1; i<=3; i++) {
            if(cur.remain-i>=0) {
                Node next=cur;
                next.path[next.step]=i;
                next.remain-=i;
                next.step++;
                q.push(next);
            }
        }
    }
}

int main() {
    int n;
    cin>>n;
    if(n<10 || n>30) {
        cout<<0;
        return 0;
    }
    bfs(n);
    cout<<cnt<<endl;
    for(int i=0; i<cnt; i++) {
        for(int j=0; j<10; j++) {
            cout<<res[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

【算法代码三:枚举法
算法竞赛中常以‌ 1 秒内完成 1 亿次(1e8)基本操作‌为参考标准
下面代码中的 10 层嵌套循环,运行次数为 3^10=59049,满足运行次数约束,可以通过。

#include<bits/stdc++.h>
using namespace std;

int n,sum;
int main() {
    cin>>n;
    for(int a=1; a<=3; a++)
        for(int b=1; b<=3; b++)
            for(int c=1; c<=3; c++)
                for(int d=1; d<=3; d++)
                    for(int e=1; e<=3; e++)
                        for(int f=1; f<=3; f++)
                            for(int g=1; g<=3; g++)
                                for(int h=1; h<=3; h++)
                                    for(int i=1; i<=3; i++)
                                        for(int j=1; j<=3; j++)
                                            if(a+b+c+d+e+f+g+h+i+j==n) sum++;
    cout<<sum<<endl;

    for(int a=1; a<=3; a++)
        for(int b=1; b<=3; b++)
            for(int c=1; c<=3; c++)
                for(int d=1; d<=3; d++)
                    for(int e=1; e<=3; e++)
                        for(int f=1; f<=3; f++)
                            for(int g=1; g<=3; g++)
                                for(int h=1; h<=3; h++)
                                    for(int i=1; i<=3; i++)
                                        for(int j=1; j<=3; j++)
                                            if(a+b+c+d+e+f+g+h+i+j==n) {
                                                cout<<a<<" ";
                                                cout<<b<<" ";
                                                cout<<c<<" ";
                                                cout<<d<<" ";
                                                cout<<e<<" ";
                                                cout<<f<<" ";
                                                cout<<g<<" ";
                                                cout<<h<<" ";
                                                cout<<i<<" ";
                                                cout<<j<<endl;
                                            }
    return 0;
}



【参考文献】
https://www.luogu.com.cn/problem/solution/P2089





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值