Lattice paths
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?
Link: https://projecteuler.net/problem=15
My solution:
Using recursie function to find out the routes.
Implemented by Lu.Qian. using C++:
1 #include <iostream> 2 //#include <cstring> 3 //#include <cmath> 4 //#include <map> 5 using namespace std; 6 7 unsigned long long int num = 0; 8 void find_route(int len, int wid){ 9 if(len == 0 || wid == 0){ 10 num++; 11 } 12 find_route(len - 1, wid); 13 find_route(len, wid - 1); 14 } 15 16 int main() 17 { 18 19 find_route(20,20); 20 cout<<num<<endl; 21 return 0; 22 }
However, this project I worte will take so much cost on recurive funciton, and the running time it take will be unacceptable.
So I‘ll wirite another function by using simple iteration.
时间: 2024-10-22 16:31:18