1. 题目
2. 解答
dp[i][j]表示机器人位于第i,j位置的时候,有多少路径
- 如果i = 0,dp[i][j] = 1;
- 如果j = 0,dp[i][j] = 1;
- 其他情况dp[i][j] = dp[i-1][j] + dp[i][j - 1]
c
#include <stdio.h>
int solve(int m, int n)
{
int dp[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m -1][n -1];
}
int main()
{
int m, n;
scanf("%d %d", &m, &n);
int result = solve(m, n);
printf("result:%d\n", result);
}
运行:
c
G3-3579:~/data/source/leetcode$ gcc 62differrentpath.c
G3-3579:~/data/source/leetcode$ ./a.out
3 7
result:28
G3-3579:~/data/source/leetcode$ ./a.out
3 2
result:3
G3-3579:~/data/source/leetcode$ ./a.out
7 3
result:28
G3-3579:~/data/source/leetcode$ ./a.out
3 3
result:6