题目链接:15.Lattice paths
题意:
Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20×20 grid?
只能往下向右走,问这样走20 * 20 的网格,走到右下点,有多少种走法?
解题思路:
就先分析2 * 2 的网格,上面有9个点。从起始点(1,1)走到(1,2)有1中走法,走到(2,1)有1种走法,,走到(3,1)有1种走法,走到(1,3)有1种走法,走到坐标点(2,2)有2种走法,走到(2, 3)有三种走法,走到(3, 2)有两种走法,最后走到(3, 3)有6中走法。
可以发现,走到(3, 3)的方法数其实就是左边点加上上边点的方法数之和。然后再往前就会发现,每个点的方法数就是左边点的方法数加上边点的方法数。而最左边和 最上变的方法数为1,
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 25;
ll a[maxn][maxn];
int main() {
int n = 21;
memset(a, 0, sizeof(a));
for(int i = 1; i <= n; i++) {
a[i][1] = 1;
a[1][i] = 1;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(a[i][j] != 0) {
continue;
} else {
a[i][j] = a[i-1][j] + a[i][j-1];
}
}
}
cout << a[n][n] << endl;
return 0;
}

本文探讨了从左上角到右下角,仅能向右或向下移动时,通过20x20网格的所有可能路径数量。通过递推公式计算,将每个点的路径数视为其左侧点和上方点路径数之和,最终得出20x20网格的总路径数。

492

被折叠的 条评论
为什么被折叠?



