码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler 15 solution

时间:2016-07-20 16:14:06      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

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. 

 

Project Euler 15 solution

标签:

原文地址:http://www.cnblogs.com/QianL/p/5688720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!