projecteuler---->problem=15----Lattice paths
2014-06-03 16:43
465 查看
Starting in the top left corner of a 2
![](https://projecteuler.net/images/symbol_times.gif)
2 grid, and only being able to move to the right and down, there are exactly 6 routes
to the bottom right corner.
![](https://projecteuler.net/project/images/p_015.gif)
How many such routes are there through a 20
![](https://projecteuler.net/images/symbol_times.gif)
20 grid?
翻译:
从一个2×2的方格的左上角开始,共有6条路径可达到右下角(不能后退)
请问,假如把上述方格的规模改成20×20,共有多少条路径呢
解析:从做PE题以来,这是第一次在一道题上卡了好一会,一开始想到的是深搜,不过没看到只能往右边和下边走,结果递归溢出。改了之后,还是跑不出结果
继续做分析如下:
![](http://img.blog.csdn.net/20140603163510875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcTc0NTQwMTk5MA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
因为只能往右和往下走,而且在一个小格中,任意位置上的路径和等于左边的点和上面的点上和,在图中的第一个小格子中,最右下角的路径和等于a+b路径上的和,那么就可以写出递归式子:
不过跑了一会还是没有跑出结果,于是我继续优化。当你求的点x,y 如果x==y的话,那么即是一个正方形的格子,正方形的格子可以对折,即左下角的点等于右上角的点,那么这里我又优化了一下,这里是一个大优化,切断了大半的递归函数。
最后貌似等了1分钟左右,终于出来了,结果是正确的,我是一个很懒的人,出了结果就不想优化了,哪个小伙伴发现了其他的方法记得留言哦!
![](https://projecteuler.net/images/symbol_times.gif)
2 grid, and only being able to move to the right and down, there are exactly 6 routes
to the bottom right corner.
![](https://projecteuler.net/project/images/p_015.gif)
How many such routes are there through a 20
![](https://projecteuler.net/images/symbol_times.gif)
20 grid?
翻译:
从一个2×2的方格的左上角开始,共有6条路径可达到右下角(不能后退)
请问,假如把上述方格的规模改成20×20,共有多少条路径呢
解析:从做PE题以来,这是第一次在一道题上卡了好一会,一开始想到的是深搜,不过没看到只能往右边和下边走,结果递归溢出。改了之后,还是跑不出结果
/* * main.c * * Created on: Jun 3, 2014 * */ #include <stdio.h> int posX[2]={ 0, 1}; int posY[2]={ 1, 0}; unsigned long count=0; int max=20; void f(int x, int y){ int i, nx, ny; if (x==max && y==max){ count++; return ; } for (i=0; i < 2; i++){ nx=x+posX[i]; ny=y+posY[i]; if (nx>=0 && nx <= max && ny>=0 && ny<=max){ f(nx,ny); } } } int main() { int i; for (i=1; i <= 10; i++) { count=0; max=i; f(0,0); printf("%ld ",count); } return 0; }
继续做分析如下:
因为只能往右和往下走,而且在一个小格中,任意位置上的路径和等于左边的点和上面的点上和,在图中的第一个小格子中,最右下角的路径和等于a+b路径上的和,那么就可以写出递归式子:
#include <stdio.h> unsigned long f(int x,int y){ if(x==0 || y==0) return 1; f(x-1,y)+f(x,y-1); } int main() { int i=1; printf("%ld ",f(20,20)); return 0; }
不过跑了一会还是没有跑出结果,于是我继续优化。当你求的点x,y 如果x==y的话,那么即是一个正方形的格子,正方形的格子可以对折,即左下角的点等于右上角的点,那么这里我又优化了一下,这里是一个大优化,切断了大半的递归函数。
#include <stdio.h> unsigned long f(int x,int y){ if(x==0 || y==0) return 1; if(x==y) return 2*f(x-1, y); else return f(x-1,y)+f(x,y-1); } int main() { int i=1; printf("%ld ",f(20,20)); return 0; }
最后貌似等了1分钟左右,终于出来了,结果是正确的,我是一个很懒的人,出了结果就不想优化了,哪个小伙伴发现了其他的方法记得留言哦!
相关文章推荐
- projecteuler---->problem=13----Large sum
- projecteuler---->problem=8----Largest product in a series
- projecteuler---->problem=35----Circular primes
- projecteuler---->problem=5----Smallest multiple n个数求最小公倍数
- projecteuler---->problem=28----Number spiral diagonals
- projecteuler---->problem=27----Quadratic primes
- projecteuler---->problem=34----Digit factorials
- projecteuler---->problem=32----Pandigital products
- projecteuler---->problem=29----Distinct powers
- projecteuler---->problem=6----Sum square difference
- projecteuler---->problem=30----Digit fifth powers
- projecteuler---->problem=22----Names scores
- projecteuler---->problem=21----Amicable numbers
- projecteuler---->problem=20----Factorial digit sum
- projecteuler---->problem=14----Longest Collatz sequence
- projecteuler---->problem=18----Maximum path sum I
- projecteuler---->problem:2
- projecteuler---->problem=4----Largest palindrome product
- projecteuler---->problem=31----Coin sums 无限背包计算可能存在的次数
- projecteuler---->problem=36----Double-base palindromes