HDU 2151 Worm

Source: Internet
Author: User
HDU 2151 wormproblem description since seeing the price rise of apple on Christmas Eve, Lele has planted a row of apple trees at the door of his house, N in total.

Suddenly Lele found a caterpillar on the P tree (counted from 1) from the left. Lele watched the apple tree for a long time to see the caterpillar change into a butterfly. Although no butterfly is seen, Lele finds a rule: every minute, the caterpillar will randomly climb from a tree to an adjacent tree.

For example, at the beginning, the caterpillar is located in 2nd trees. After one minute, the caterpillar may be located in 1st trees or 3rd trees. If at the beginning the caterpillar was on 1st trees, after one minute, the caterpillar would certainly be on 2nd trees.

I will tell you the number n of apple trees and the position P where Mao was at the beginning. How many walking solutions will the caterpillar reach the T tree in M minutes.

Input this question contains multiple groups of tests. Please process it until the end of the file (EOF ).
Each group of tests occupies one row, including four positive integers N, P, M, and T (for meanings, see the topic description, 0 <N, P, M, T <100)

Output outputs the total number of solutions in one row for each group of data.
Question data guarantee answer less than 10 ^ 9

Sample input3 2 4 2 2 3 2

Sample output4 0

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2151

 

Solution: 1. there are two kinds of thinking derivation results for this question: derivation from the past to the next, and derivation from the later to the next. derived from the past: Set DP [I] [J]: the time of the previous I minutes, the sum of the walk to the J-tree. DP [I] [J] = DP [I-1] [J + 1] + dp [I-1] [J-1]; (the boundary problem is to remove half .) the obvious result is: DP [m] [T]; derived from the back: Set DP [I] [J]: return I minutes from time m, the sum of steps to reach the J-tree. DP [I] [J] = DP [I + 1] [J + 1] + dp [I + 1] [J-1]; Result: DP [1] [p];

 

# Include <iostream>
Using namespace STD;
Int DP [102] [102];
Int main ()
{
Int N, P, M, T;
While (~ Scanf ("% d", & N, & P, & M, & T ))
{
Int I, J;
Memset (DP, 0, sizeof (DP ));
DP [0] [p] = 1;
For (I = 1; I <= m; I ++)
For (j = 1; j <= N; j ++)
DP [I] [J] = DP [I-1] [J-1] + dp [I-1] [J + 1];
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.