URAL--DFS finding law-nudnik photographer

Source: Internet
Author: User

Description

If two people were born one after another with one second difference and one of them is a child, then the other one is a child too. we get by induction that all the people are children. everyone knows that the mathematical department of the Ural State University is a big family NPersons, 1, 2, 3 ,..., NYears old respectively. once the Dean of the Department ordered a photo if his big family. there were to be present all the students of the Department arranged in one row. at first the Dean wanted to arrange them by their age starting from the youngest student, but than he decided that it wocould look unnatural. than he advised to arrange the students as follows:
  1. The 1 year old student is to sit at the left end of the row.
  2. The difference in ages of every two neighbors mustn't exceed 2 years.
The Dean decided that thereby the students wowould seem look as they were arranged by their ages (one can hardly see the difference in ages of 25 and 27 years old people ). there exist several arrangements satisfying to the requirements. photographer didn't want to thwart Dean's desire and made the photos of all the possible mathematical department students 'arrangements.

Input

There is the integer number N, 1 ≤ N≤ 55.

Output

The number of photos made by the photographer.

Sample Input

Input Output
4
4
Notesif N= 4 then there are following possible arrangements: (,), (,), (,) and ). the first person must be 1-year-old, and the remaining age must be no more than 2-year-old .. It is DP .. Obtain DP [N] = DP [n-1] + dp [n-3] + 1 view Portal
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int vis[60];int n;int ans;void dfs(int m,int num){    if(num == n){        ans++;        return ;    }    for(int i = m - 2; i <= m + 2; i++){        if(i <= 1 || i > n )            continue;        if(!vis[i]){            vis[i] = 1;            dfs(i,num+1);            vis[i] = 0;        }    }}int main(){    while(~scanf("%d",&n)){        ans = 0;        memset(vis,0,sizeof(vis));        vis[1] = 1;        dfs(1,1);        printf("%d\n",ans);    }    return 0;}

AC code

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[60];int main(){    int n;    dp[1] = 1;    dp[2] = 1;    dp[3] = 2;    while(~scanf("%d",&n)){        for(int i = 3; i <= n ;i++)            dp[i] = dp[i-1] + dp[i-3] + 1;       printf("%d\n",dp[n]);    }    return 0;}

  

URAL--DFS finding law-nudnik photographer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.