计数型动态规划dp

114. 不同的路径
描述
有一个机器人的位于一个 m × n 个网格左上角。
机器人每一时刻只能向下或者向右移动一步。机器人试图达到网格的右下角。
问有多少条不同的路径?
n和m均不超过100
且答案保证在32位整数可表示范围内。
输入 m,n表示行列
输出可能的路径个数

样例
Example 1:

Input: n = 1, m = 3
Output: 1
Explanation: Only one path to target position.
Example 2:

Input: n = 3, m = 3
Output: 6
Explanation:
D : Down
R : Right
1) DDRR
2) DRDR
3) DRRD
4) RRDD
5) RDRD
6) RDDR
#include<stdio.h>
int dp(int m,int n){
int f[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i0||j0)
f[i][j] = 1;
else
f[i][j] = f[i-1][j] + f[i][j-1];
}
}
return f[m-1][n-1];
}
int main(){
int m,n;
scanf("%d%d",&m,&n);
printf("%d\n",dp(m,n));
return 0;
}

发布了15 篇原创文章 · 获赞 13 · 访问量 674

猜你喜欢

转载自blog.csdn.net/qq_43324779/article/details/102261098