作业-2025-12-19

· · 个人记录

先讲一下知识:全排列

如果我们要枚举几个数,那么大家一定会想到循环嵌套:

for(int i=1;i<=n;i++)
{
    for(int j=1;j<=n j++)
    {
        if(i==j)
        {
            continue;
        }
        //后续代码
    }
}

但是,循环嵌套只能解决层数固定的枚举,那么我们就只能寻找别的方法。这时,发现递归可以解决,那么我们写一个递归函数:

void dfs(int step)
{
    if(step>n)//如果循环超过n层,那么结束。
    {
        return ;
    }
    //后续代码
}

第一题(T712090 dfs-全排列-1)

思路:

本题非常简单,只是基础全排列,不用多讲,dfs

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=15;
int n,k,a[N];
bool f[N];
void dfs(int step)
{
    if(step>k)
    {
        for(int i=1;i<=k;i++)
        {
            cout<<a[i]<<' ';
        }
        cout<<'\n';
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(f[i])
        {
            continue;
        }
        f[i]=true;
        a[step]=i;
        dfs(step+1);
        f[i]=false;
    }
    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n>>k;
    dfs(1);
    return 0;
 }
//c++14 whithO2

第二题(T712153 dfs-全排列-2)

思路:

我们有两种方法:

1.先枚举出每个分发礼物的可能,再查找合法方案。

2.只枚举合法方案,按字典序从小到大枚举,当step>n时就说明有正确方案,输出,否则没有方案满足,输出-1

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=15;
int n,m,a[N],b[N],ans[N];
bool flag,f[N];
void dfs(int step)
{
    if(flag==true)
    {
        return ;
    }
    if(step>m)
    {
        flag=true;
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(f[i]==true || a[i]<b[step] || flag==true)
        {
            continue;
        }
        f[i]=true;
        ans[step]=i;
        dfs(step+1);
        f[i]=false;
    }
    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=m;i++)
    {
        cin>>b[i];
    }
    dfs(1);
    if(flag==false)
    {
        cout<<"-1";
        return 0;
    }
    for(int i=1;i<=m;i++)
    {
        cout<<ans[i]<<' ';
    }
    return 0;
 }
//c++14 whithO2

第三题(T712197 dfs-全排列-3)

思路:

本题可以使用dfs进行枚举,枚举出所有安排超人的排列,再算出需要多少间能量仓,最后统计最小值。根据题意可以发现,两个超人之间相隔的能量仓数量是

max(a[pl[i-1]].r,a[pl[i]].l)

然后,本来就有n个超人要使用能量仓,得到计算方法:

int cnt=n;
for(int i=2;i<=n;i++)
{
  cnt+=max(a[pl[i-1]].r,a[pl[i]].l);
}
ans=min(ans,cnt);
//c++14 whithO2

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=15;
int n,ans=1e7,pl[N];
bool f[N];
struct node{
    int l,r;
}a[N];
void dfs(int step)
{
    if(step>n)
    {
        int cnt=n;
        for(int i=2;i<=n;i++)
        {
            cnt+=max(a[pl[i-1]].r,a[pl[i]].l);
        }
        ans=min(ans,cnt);
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(f[i]==true)
        {
            continue;
        }
        f[i]=true;
        pl[step]=i;
        dfs(step+1);
        f[i]=false;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].l;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].r;
    }
    dfs(1);
    cout<<ans;
    return 0;
 }
//c++14 whithO2

第四题(T712203 dfs-全排列-4)

思路:

题目给了我们n个调整后可能的形态,让我们求出可能有多少种形态。所以我们可以先枚举完所有的可能的密码 (防止遗漏) 再枚举看是不是符合要求注意, 答案要满足任意一个调整后的数列

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=15,m=5;
int n,a[N][N],ans[N];
bool f[N];
int ans2=0;
bool check(int x)
{
    int cnt=0;
    for(int i=1;i<=5;i++)
    {
        if(ans[i]!=a[x][i])
        {
            cnt++;
            if(cnt==2)
            {
                if(ans[i-1]==a[x][i-1])
                {
                    return false;
                }
                int v=(ans[i]-a[x][i]+10)%10;
                int v1=(ans[i-1]-a[x][i-1]+10)%10;
                if(v!=v1)
                {
                    return false;
                }
            }
            else if(cnt>2)
            {
                return false;
            }
        }
    }
    if(cnt==0)
    {
        return false;
    }
    return true;
}
void dfs(int step)
{
    if(step>5)
    {
        for(int i=1;i<=n;i++)
        {
            if(check(i)==false)
            {
                    return ;
            }
        }
        ans2++;
        return ;
    }
    for(int i=0;i<=9;i++)
    {
        ans[step]=i;
        dfs(step+1);
    }
    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n;
    if(n==1)
    {
        cout<<81;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    dfs(1);
    cout<<ans2;
    return 0;
}
//c++14 whithO2

第五题(T712239 dfs-全排列-5)

思路:

首先,我们枚举出所有可能成立的序列(每一项从1~n,不重复)然后再递推计算“魔法数”,看是否符合,按字典序从小到大枚举,遇到就输出、结束程序exit(0)但是时间复杂度太高了,O(n!*n^2),需要优化。

我们发现,“魔法数”是由多个数的和构成的,那么我们可以算出每个数累加了多少次,快速得出“魔法数”。

数权值:

a b c d e

a+b b+c c+d d+e

a+2b+c b+2c+d c+2d+e

a+3b+3c+d b+3c+3d+e

a+4b+6c+4d+e

而杨辉三角(倒过来):

1 4 6 4 1

1 3 3 1

1 2 1

1 1

1

可以发现,各个数权值对应了倒过来的杨辉三角。那么我们可以预处理杨辉三角优化。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=15;
long long n,m,ans[N],f[N][N];
bool vis[N];
void dfs(int step, int sum)
{
    if(step > n)
    {
        if(sum == m)
        {
            for(int i = 1; i <= n; i ++)
            {
                cout << ans[i] << ' ';
            }
            exit(0);
        }
        return ;
    }
    for(int i = 1; i <= n; i ++)
    {
        if(vis[i] == 0 && sum + f[n][step] * i <= m)
        {
            vis[i] = 1;
            ans[step] = i;
            dfs(step + 1, sum + f[n][step] * i);
            vis[i] = 0;
        }
    }
}
int main()
{
    cin >> n >> m;
    f[1][1] = 1;
    for(int i = 2; i <= n; i ++)
    {
        for(int j = 1; j <= i; j ++)
        {
            f[i][j] = f[i - 1][j - 1] + f[i - 1][j];
        }
    }
    dfs(1, 0);
    return 0;
}
//c++14 whithO2