洛谷 P1118 dfs搜索

https://www.luogu.org/problemnew/show/P1118

题目描述

FJ and his cows enjoy playing a mental game. They write down the numbers from 111 toN(1≤N≤10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:

    3   1   2   4
      4   3   6
        7   9
         16

Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.

Write a program to help FJ play the game and keep up with the cows.

有这么一个游戏:

写出一个1至N的排列ai,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置。下面是一个例子:

3,1,2,4

4,3,6

7,9

16

最后得到16这样一个数字。

现在想要倒着玩这样一个游戏,如果知道N,知道最后得到的数字的大小sum,请你求出最初序列ai​,为1至N的一个排列。若答案有多种可能,则输出字典序最小的那一个。

[color=red]管理员注:本题描述有误,这里字典序指的是1,2,3,4,5,6,7,8,9,10,11,12

而不是1,10,11,12,2,3,4,5,6,7,8,9[/color]

输入输出格式

输入格式:

两个正整数n,sum。

输出格式:

输出包括1行,为字典序最小的那个答案。

当无解的时候,请什么也不输出。(好奇葩啊)

输入输出样例

输入样例#1: 复制

4 16

输出样例#1: 复制

3 1 2 4

说明

对于40%的数据,n≤7

对于80%的数据,n≤10

对于100%的数据,n≤12,sum≤12345

思路:从小到大枚举n的全排列,如果有满足题意的情况,就返回。当然枚举的过程可以自己实现,也可以用stl里面的next_permutation来实现。另外一提,我们假设n=4,排列为a、b、c、d,那么按照题意可以得到sum=a+3*b+3*c+d,同理可以计算其他情况,会发现系数很有规律的!其实就是杨辉三角。因此计算sum的值就变的很简单了。注意剪枝。

#include<iostream>
#include<cstdio>
using namespace std;

int a[15][15];  //杨辉三角系数
int re[15];     //结果
int vis[15];    //标记一个数是否用过
int flag=0; //标记是否已经找到解
int n,sum;

void dfs(int cur,int v);//cur表示当前要处理第几个数 v表示当前的和

int main()
{
    scanf("%d%d",&n,&sum);
    for(int i=1;i<=n;i++)   //杨辉三角
        a[i][1]=a[i][i]=1;
    for(int i=3;i<=n;i++)   //杨辉三角
        for(int j=2;j<i;j++)
            a[i][j]=a[i-1][j]+a[i-1][j-1];
    dfs(1,0);
    return 0;
}

void dfs(int cur,int v)
{
    if(v>sum||(v==sum&&cur<=n)||flag)//剪枝
        return ;
    if(cur==n+1)
    {
        if(v==sum)//满足题意的情况
        {
            for(int i=1;i<=n;i++)
                printf("%d ",re[i]);
            flag=1;
        }
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i])
            continue;
        vis[i]=1;   //标记i已经用过
        re[cur]=i;
        dfs(cur+1,v+a[n][cur]*i);
        vis[i]=0;   //记得把标记改回来
    }
    return ;
}

下面这个是用next_permutation来生成下一个排列, 这个剪枝跟上面一个不太一样, 比如当前序列为:1、2……i……, 且依据题意算出来前i项的和已经超过sum了, 那么再对i以后(包括i)生成的更大的排列必定会超过sum, 因此这时候对序列i项以后(包括第i项)进行从大到小的排序, 这样next_permutation会直接跳过这一系列无意义的序列。 花的时间比上面的做法要多。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int a[15][15];
int re[15];
int flag=0;
int n,sum;

int main()
{
    scanf("%d%d",&n,&sum);
    for(int i=1;i<=n;i++)   //杨辉三角
    a[i][1]=a[i][i]=1;
    for(int i=3;i<=n;i++)   //杨辉三角
        for(int j=2;j<i;j++)
            a[i][j]=a[i-1][j]+a[i-1][j-1];
    for(int i=1;i<=n;i++)
        re[i]=i;
    do
    {
        if(flag)    //已经找到一组解了
            break;
        int temp=0;
        for(int i=1;i<=n;i++)
        {
            temp+=a[n][i]*re[i];
            if(temp>sum)    //超过了sum
            {
                sort(re+i,re+n+1,greater<int>());//对i项以后(包括i)进行排序
                break;
            }
        }
        if(temp==sum)
        {
            for(int i=1;i<=n;i++)
                printf("%d ",re[i]);
            flag=1;
        }
    }while(next_permutation(re+1,re+1+n));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/87826559