POJ3187 Backward Digit Sums

给出杨辉三角的顶点值,求底边各个数的值。直接DFS就好了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define ll __int64
#define INF 0x3fffffff
#define rep(i,n) for(int (i)=0;(i)<n;(i)++)
using namespace std;
int n,k,sum;
int a[15][15];
bool vis[12];

bool judge()
{
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<n-i;j++)
        {
            a[i][j]=a[i-1][j]+a[i-1][j+1];
        }
    }
    if(a[n-1][0]==sum) return true;
    return false;
}

bool dfs(int cur)
{
    if(cur==n)
    {
        if(judge()) return true;
        return false;
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            a[0][cur]=i;
            vis[i]=true;
            if(dfs(cur+1)) return true;
            a[0][cur]=0;
            vis[i]=false;
        }
    }
    return false;
}

int main()
{
    cin>>n>>sum;
    k=0;
    memset(a,0,sizeof(a));
    memset(vis,false,sizeof(vis));
    dfs(0);
    rep(i,n)
    {
        cout<<a[0][i];
        if(i==n-1) break;
        cout<<" ";
    }
    cout<<endl;
    return 0;
}
时间: 2024-12-18 21:43:53

POJ3187 Backward Digit Sums的相关文章

POJ3187 Backward Digit Sums 【暴搜】

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 2575 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum ad

(DFS、全排列)POJ-3187 Backward Digit Sums

题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难推得第一行为n个数a1\a2\--\an时求得的和为i=0∑n-1 ai*(n-1Ci) 根据此公式,考虑到数据量比较小,只需要将原本按递增顺序依次排列好的1--n按next_permutation给出的递增全排列顺序逐个代入,如果结果与m相等就停止循环即可. 参考代码: 1 #include<st

【POJ - 3187】Backward Digit Sums(搜索)

-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然后把相邻的数相加的到新的一行数.重复这一操作直至只剩一个数字.比如下面是N=4时的一种例子 3 1 2 4 4 3 6 7 9 16 在FJ回来之前,奶牛们开始了一个更难的游戏:他们尝试根据最后结果找到开始的序列.这已超过了FJ的思考极限. 写一个程序来帮助FJ吧 Input N和最后的和 Output 满足

bzoj1653:Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 318  Solved: 239[Submit][Status][Discuss] Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a cer

BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved: 161[Submit][Status][Discuss] Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a cer

POJ 3187 Backward Digit Sums(next_permutation)

Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (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

Backward Digit Sums(暴力)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 3280 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum ad

POJ 题目Backward Digit Sums(next_permutation)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4807   Accepted: 2772 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum ad

Backward Digit Sums(POJ-3187)

本来以为会超时,看来是想多了,63ms,看来对时间复杂度的判断能力还是不行啊.这个题正是next_permutation()函数的用武之地. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<cmath> using namespace std; int n,sum,a[100],A[10