HDU 2067 小兔的棋盘

http://acm.hdu.edu.cn/showproblem.php?pid=2067

Problem Description
小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望。不过没过几天发现了棋盘的好玩之处。从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?小兔想了很长时间都没想出来,现在想请你帮助小兔解决这个问题,对于你来说应该不难吧!
 
Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。
 
Output
对于每个输入数据输出路径数,具体格式看Sample。
 
Sample Input
1
3
12
-1
 
Sample Output
1 1 2
2 3 10
3 12 416024

 代码:

#include <bits/stdc++.h>
using namespace std;

long long dp[45][45];

int main() {
    int n,cnt=1;
    while(~scanf("%d",&n)) {
        if(n==-1) break;
            printf("%d %d ",cnt,n);
         for(int j=0;j<=n-1;j++)
            dp[n][j]=1;
    for(int i=n-1;i>=0;i--) {
        for(int j=i;j>=0;j--) {
            if(i==j)
                dp[i][j]=dp[i+1][j];
            else
                dp[i][j]=dp[i+1][j]+dp[i][j+1];
        }
    }
    printf("%lld\n",2*dp[0][0]);
    cnt++;
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zlrrrr/p/9364050.html