projectEuler Problem 15.Lattice paths

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

题目链接: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;
}

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值