"Algorithmic Learning Notes" 27. Dynamic planning Problem Solving report Sjtu_oj 1254 pass Handkerchief

Source: Internet
Author: User

1254. Pass the handkerchief description

During the event, the teacher often took the students to play together. This time, the teacher with the students to pass the handkerchief together.

The rules of the game are like this: N students stand in a circle, one of the students holding a handkerchief, when the teacher blew the whistle began to preach, each classmate can pass the handkerchief to their own about two students of one (left or right), when the teacher in this blow whistle, the game stopped, at this time, The classmate holding the handkerchief is going to show you a show.

ABC raises an interesting question: how many different ways of passing a handkerchief can make a handkerchief that starts in the hands of ABC, passes the M-time, and goes back to ABC's hand. Two kinds of handkerchief methods are treated as different methods, and only when the two methods, the students received the ball by the order of the handkerchief sequence is different. For example, there are three students 1th, 2nd, 3rd, and assume that ABC is 1th, the handkerchief passed 3 times back to the ABC hand of the way there are 1->2->3->1 and 1->3->2->1, a total of 2.

Input Format

A total of two integer n,m (3<=n<=30,1<=m<=30) separated by a space.

Output Foramt

A total row that has an integer that represents the number of methods that conform to test instructions.

Sample Input
Sample Output
2
Hint

40% of the data meet: 3<=n<=30,1<=m<=20 100% data to meet: 3<=n<=30,1<=m<=30

This problem is easily reminiscent of the adjacency matrix multiplication meaning. Wiki, this kind of problem is called "passing problem."

The proof here that matrix multiplication is really good to understand but the actual application is slightly more complex,

First, a recursive scheme is used to parse the problem.

Notice that a person passes, can only pass the left one and the right one, according to this breakthrough point can be written as a recursive function.

/*divide-and-conquer method returns a total of n individuals and the number of M passes left in the K-Man's hands. K is the person who is currently taking the ball number 1th is the person who started to get the ball.*/intPassgame (intKintNintm) {    if(m==0)        //If there are 0 passes left, then return 1 means the original route is right or 0 .        return(k==1) ?1:0; M--;//so we're going to have to cut the ball a few times.//If you preach to +1,    intans1 = Passgame (k==n?1: K +1), n,m); //If you preach to 1,    intAns2 = Passgame ((k==1? n:k-1), n,m); returnans1+Ans2;} 
Divide and conquer the law

Then call Passgame (1,n,m) to

Will time out, the solution to this divide-and-conquer approach is from top to bottom. It's going to have a lot of repetitive calculations, so we'll think of a dynamic plan that's the opposite of the divide-and-conquer route.

A core idea of dynamic programming is to calculate the repetition sub-problem in advance, thus reducing the computational amount. This means that it has to be calculated from the bottom up.

In this context, we may intuitively think of setting a dp[n][m] where d[n][m] represents the number of programs that n people pass M-times back to the origin.

Then find a way to establish the relationship between D[n][m] and d[n][m-1] or the relationship with D[n-1][m] or d[n-1][m-1], so that the problem can be divided into several sub-problems for DP.

But finding the relationship between them is very difficult.

We try to change a way of thinking, by the division of the Law of thought, we know that one thing is still left the S pass chance, the ball to the K-person hand of the number of routes = there is still s-1 times the passing chance, the ball to the k+1 person in the hands of the number of routes + s-1 pass opportunity, to reach the k-1 of the number

PS s-1 The reason is that the end of a passing chance to pass the ball to the K hand k+1 k-1 just simple to say about K

So we can consider constructing a dp[][] to store the number of routes left in the K-man's hands. The DP scheme is to reverse the divide and conquer. The idea is exactly the same.

//Dynamic Programming Algorithmintdp[ *][ *]={0};/*Dp[i][j] Indicates that the number of the starting and ending points of the route to the first J-person is 1th.*/intDp_game (intNintm) {dp[0][1]=1;//Pass 0 times to number 1th, there's only one route.//start looking for state transition equations    /*in fact, the idea is very simple is to think D[i][j] is from which state come over d[i][j] should be d[i-1][j-1] and d[i-1][j+1 "because J can only receive j-1 and J+1 ball and each pass happens I+1 0 times the ball only No. 1th is the only one who gets the ball. The others are 0.*/     for(inti =1; I <= m; ++i) {         for(intj=1; J <= N; ++j) {Dp[i][j]= dp[i-1][j==1? n:j-1] + dp[i-1][j==n?1: j+1]; }    }    returndp[m][1];}
Dynamic Planning

"Algorithmic Learning Notes" 27. Dynamic planning Problem Solving report Sjtu_oj 1254 pass Handkerchief

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.