欧拉计划第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?

网格路径

从一个2×2方阵的左上角出发,只允许向右或向下移动,则恰好有6条通往右下角的路径。

对于20×20方阵来说,这样的路径有多少条?

解题思路

动态规划。

\(f[i][j] = f[i-1][j] + f[i][j-1]\)

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
long long f[21][21];
int main() {
    for (int i = 1; i <= 20; i ++) {
        for (int j = 1; j <= 20; j ++) {
            if (i==1 || j==1) f[i][j] = 1;
            else f[i][j] = f[i-1][j] + f[i][j-1];
        }
    }
    cout << f[20][20] << endl;
    return 0;
}

答案为 \(35345263800\)

posted @ 2020-02-18 21:07  quanjun  阅读(230)  评论(0编辑  收藏  举报