Worm (caterpillar) HDU2151

Source: Internet
Author: User
Problem Description:Since seeing the price of Apple on Christmas Eve, Lele has planted a row of apple trees at his doorstep, with a total of n trees. Suddenly Lele found a caterpillar on the P tree (starting from 1) on the left. In order to see the caterpillar become a butterfly process, Lele at the apple tree to observe for a long time. Although no butterflies were seen, Lele found a pattern: every minute, the caterpillar would randomly crawl from tree to tree adjacent to it. For example, in the first caterpillar in the 2nd tree, after a minute, the worm may be in the 1th tree or the 3rd tree, if the caterpillar in the beginning of a tree, a minute later, the caterpillar will be in the 2nd tree. Now tell you the number of apple trees N, as well as the beginning of the caterpillar position p, ask, in M minutes, after the caterpillar arrives at the T tree, there are altogether how many ways to walk the number of programs.
Enter Example 1:3 2 4 2 Output Example 1:4

Tip, the above 4 methods are as follows:

2->1->2->1->2
2->1->2->3->2
2->3->2->1->2
2->3->2->3- >2

The code is as follows:

/*
now tell you the number of apple trees N, as well as the beginning of the caterpillar position p, in M minutes, the caterpillar arrives at the T tree, total number of ways to walk
*/ 
#include <cstdio> 
#include <cstring> 
int Main () { 
    int n,p,m,t,i,j; 
    while (scanf ("%d%d%d%d", &n,&p,&m,&t)!=eof) { 
        int dp[110][110]; 
        Memset (Dp,0,sizeof (DP)); 
        Dp[0][p]=1;   
        for (i=1;i<=m;++i)     //i minutes for 
            (J=1;J<=N;++J)//J Position 
                dp[i][j]=dp[i-1][j+1]+dp[i-1][j-1];// The total number of mobile schemes that represent the first minute to the J position is the sum of the mobile schemes on both sides of 
        printf ("%d\n", Dp[m][t]); 
    } 
    return 0; 
} 


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.