poj 3187 Backward Digit Sums(穷竭搜索dfs)


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 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.

Input

Line 1: Two space-separated integers: N and the final sum.

Output

Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

Sample Input

4 16

Sample Output

3 1 2 4

Hint

Explanation of the sample: 
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.

Source

USACO 2006 February Gold & Silver

注意杨辉三角的求法,要用二维来求,其它的就没什么了。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 16
 6 int n,sum;
 7 int vis[N];
 8 int a[N][N];
 9 int flag;
10 bool check(){
11     for(int i=1;i<n;i++){
12         for(int j=0;j<n-i;j++){
13             a[i][j]=a[i-1][j]+a[i-1][j+1];
14         }
15     }
16     return a[n-1][0]==sum;
17 }
18 void dfs(int num){
19
20     if(num==n){
21         if(check())
22           flag=1;
23         return;
24     }
25     for(int i=1;i<=n;i++){
26         if(!vis[i]){
27             vis[i]=1;
28             a[0][num]=i;
29             dfs(num+1);
30             if(flag==1)
31               return;
32             vis[i]=0;
33         }
34     }
35 }
36 int main()
37 {
38     while(scanf("%d%d",&n,&sum)==2){
39
40         if(n==1)
41         {
42             printf("%d\n",sum);
43             continue;
44         }
45
46         memset(vis,0,sizeof(vis));
47         flag=0;
48         dfs(0);
49         for(int i=0;i<n;i++){
50             printf("%d",a[0][i]);
51             if(i!=n-1)
52               printf(" ");
53         }
54         printf("\n");
55     }
56     return 0;
57 }

时间: 2024-10-27 01:24:47

poj 3187 Backward Digit Sums(穷竭搜索dfs)的相关文章

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

POJ 3187 Backward Digit Sums

http://poj.org/problem?id=3187 穷竭搜索 全排列 然后按规则求和 排列之前先按升序排序 这样可以保证第一个和为k的就是符合最小序列的结果 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int sum(int a[], int n) 8 { 9 int s[11]; 10 for (int i = 0

POJ 3187 Backward Digit Sums 题解 《挑战程序设计竞赛》

题目:POJ 3187 思路: 这道题很简单,用next_permutation枚举1~N的所有排列,然后依次相加,判断最后的和是否等于sum,是的话则break,即为字典序最前的. 1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int n; 7 int sum; 8 int mycase[11][11]; 9 10 int main() { 11 cin >> n &

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)

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 is left. Fo

POJ 2718 Smallest Difference (穷竭搜索)

http://poj.org/problem?id=2718 将一个数切一刀拆成两个数,两个数每一位数字的顺序都可改变,但是不能有前导0.求这两个数之差的最小值. 我使用了搜索并且避免了递归,自认为是比较好的算法. 一开始我想到的是枚举,并且很快给出了实现: #ifndef ONLINE_JUDGE #pragma warning(disable : 4996) #endif #include <iostream> #include <string> #include <al

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

poj 2718 Smallest Difference(穷竭搜索dfs)

Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the

【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 满足