csustOJ1077走格子(基础DP)

Description

对于一个n*m的方格,你初始时站在左上角,你每次移动只能往下或向右移动一个单位,问你从左上角移动到右下角的方案有多少种。

Input

多组输入。

每组数据输入两个数n,m(1<=n,m<=30)。

Output

方案数。

Sample Input 1

1 1
2 2
Sample Output 1

2
6
Hint

思路:定义dp[n][m]中为格子走到(n,m)的方案数,由此得到转移方程 dp[i][j] = dp[i - 1][j] + dp[i][j - 1];注意边界上的格子初始化为1,代表初始状态。

#include <iostream>
#include <cstdio>

using namespace std;

long long dp[35][35];

int main(int argc, char *argv[]) 
{
	int n,m;
	while(~scanf("%d%d",&n,&m))	
	{
		for(int i = 0;i<=n;i++)
		{
			dp[i][0] = 1;
		}
		for(int j = 0;j<=m;j++)
		{
			dp[0][j] = 1;
		}
		
		for(int i = 1;i<=n;i++)	
		{
			for(int j = 1;j<=m;j++)
			{
				dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
			}
		}
		
		printf("%lld
",dp[n][m]);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/tomjobs/p/10612571.html