Problem 15:Lattice paths

本文介绍了使用动态规划解决网格路径问题的方法,具体以2020网格为例,通过构建状态转移方程计算从左上角到右下角的路径数量。同时提供了PHP代码实现,详细解释了算法逻辑与步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接:http://projecteuler.net/problem=15

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的网格,这样的路有多少条?


解法1:

规律看这里:http://www.robertdickau.com/lattices.html

all paths through 4-by-4 lattice superimposed

php代码:

$len = 21;
$arr=array(0=>array_fill(0,$len,1));

for($i=1;$i<$len;$i++){
	$arr[$i][0]=1;
	for($j=1;$j<$len;$j++){		
		$arr[$i][$j] = $arr[$i][$j-1]+$arr[$i-1][$j];
	}
}


注:题目的中文翻译源自http://pe.spiritzhang.com


> # 1. 确保已安装BiocManager > if (!require("BiocManager", quietly = TRUE)) + install.packages("BiocManager") Bioconductor version 3.20 (BiocManager 1.30.26), R 4.4.3 (2025-02-28 ucrt) Bioconductor version '3.20' is out-of-date; the current release version '3.21' is available with R version '4.5'; see https://bioconductor.org/install Warning messages: 1: In get(method, envir = home) : internal error 1 in R_decompress1 with libdeflate 2: In get(method, envir = home) : internal error 1 in R_decompress1 with libdeflate 3: In get(method, envir = home) : internal error 1 in R_decompress1 with libdeflate > # 2. 安装AgilMicroRna及其依赖 > BiocManager::install("AgilMicroRna") 'getOption("repos")' replaces Bioconductor standard repositories, see 'help("repositories", package = "BiocManager")' for details. Replacement repositories: CRAN: https://mirrors.tuna.tsinghua.edu.cn/CRAN/ Bioconductor version 3.20 (BiocManager 1.30.26), R 4.4.3 (2025-02-28 ucrt) Installing package(s) 'AgilMicroRna' Installation paths not writeable, unable to update packages path: C:/Program Files/R/R-4.4.3/library packages: cluster, foreign, lattice, MASS, Matrix, mgcv, nlme Old packages: 'curl', 'patchwork', 'Rcpp', 'restfulr' Update all/some/none? [a/s/n]: # 3. 验证安装 Update all/some/none? [a/s/n]: library(AgilMicroRna) Update all/some/none? [a/s/n]: packageVersion("AgilMicroRna") # 应返回版本号如'2.46.0' Update all/some/none? [a/s/n]: library(dplyr) Update all/some/none? [a/s/n]: a Warning: 正在使用‘patchwork’这个程序包,因此不会被安装 trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.4/curl_6.4.0.zip' Content type 'application/zip' length 3662693 bytes (3.5 MB) downloaded 3.5 MB trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.4/Rcpp_1.1.0.zip' Content type 'application/zip' length 2902825 bytes (2.8 MB) downloaded 2.8 MB trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.4/restfulr_0.0.16.zip' Content type 'application/zip' length 392970 bytes (383 KB) downloaded 383 KB 程序包‘curl’打开成功,MD5和检查也通过 Warning: 无法删除软件包 ‘curl’ 的先前安装 Warning: 回复了‘curl’ 程序包‘Rcpp’打开成功,MD5和检查也通过 Warning: 无法删除软件包 ‘Rcpp’ 的先前安装 Warning: 回复了‘Rcpp’ 程序包‘restfulr’打开成功,MD5和检查也通过 Warning: 无法删除软件包 ‘restfulr’ 的先前安装 Warning: 回复了‘restfulr’ 下载的二进制程序包在 C:\Users\huang\AppData\Local\Temp\RtmpMtJX8K\downloaded_packages里 Warning messages: 1: package ‘AgilMicroRna’ is not available for Bioconductor version '3.20' A version of this package for your version of R might be available elsewhere, see the ideas at https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages 2: In file.copy(savedcopy, lib, recursive = TRUE) : problem copying C:\Users\huang\AppData\Local\R\win-library\4.4\00LOCK\curl\libs\x64\curl.dll to C:\Users\huang\AppData\Local\R\win-library\4.4\curl\libs\x64\curl.dll: Permission denied 3: In file.copy(savedcopy, lib, recursive = TRUE) : problem copying C:\Users\huang\AppData\Local\R\win-library\4.4\00LOCK\Rcpp\libs\x64\Rcpp.dll to C:\Users\huang\AppData\Local\R\win-library\4.4\Rcpp\libs\x64\Rcpp.dll: Permission denied 4: In file.copy(savedcopy, lib, recursive = TRUE) : problem copying C:\Users\huang\AppData\Local\R\win-library\4.4\00LOCK\restfulr\libs\x64\restfulr.dll to C:\Users\huang\AppData\Local\R\win-library\4.4\restfulr\libs\x64\restfulr.dll: Permission denied > library(AgilMicroRna) Error in library(AgilMicroRna) : 不存在叫‘AgilMicroRna’这个名称的程序包
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值