蓝桥---传球游戏(dp)

 

Description

  上体育课的时候,小蛮的老师经常带着同学们一起做游戏。这次,老师带着同学们一起做传球游戏。
游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同学可以把球传给自己左右的两个同学中的一个(左右任意),当老师再次吹哨子时,传球停止,此时,拿着球没传出去的那个同学就是败者,要给大家表演一个节目。
聪明的小蛮提出一个有趣的问题:有多少种不同的传球方法可以使得从小蛮手里开始传的球,传了m次以后,又回到小蛮手里。两种传球的方法被视作不同的方法,当且仅当这两种方法中,接到球的同学按接球顺序组成的序列是不同的。比如有3个同学1号、2号、3号,并假设小蛮为1号,球传了3次回到小蛮手里的方式有1->2->3->1和1->3->2->1,共2种。

Input

  共一行,有两个用空格隔开的整数n,m(3<=n<=30,1<=m<=30)。

Output

  t共一行,有一个整数,表示符合题意的方法数。

Sample Input

3 3

Sample Output

2

HINT

数据规模和约定

40%的数据满足:3<=n<=30,1<=m<=20

100%的数据满足:3<=n<=30,1<=m<=30

 

明显dp问题,而且比较好想

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <stack>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const int maxn=1e7+10;
17 using namespace std;
18 
19 int dp[50][50];//dp[i][j]表示传i次到j号有多少种方法 
20 
21 int main()
22 {
23     int n,m;
24     scanf("%d %d",&n,&m);
25     for(int i=1;i<=n;i++)//预处理 
26     {
27         if(i==1||(i!=2&&i!=n))
28             dp[1][i]=0;
29         else
30             dp[1][i]=1;
31     }
32     for(int i=2;i<=m;i++)
33     {
34         for(int j=1;j<=n;j++)
35         {
36             if(j==1)
37                 dp[i][j]=dp[i-1][n]+dp[i-1][j+1];
38             else if(j==n)
39                 dp[i][j]=dp[i-1][j-1]+dp[i-1][1];
40             else
41                 dp[i][j]=dp[i-1][j-1]+dp[i-1][j+1];
42         }
43     }
44     printf("%d\n",dp[m][1]);
45     return 0;
46 }

 

posted @ 2019-11-16 18:29  jiamian22  阅读(195)  评论(0编辑  收藏  举报