Problem 15
http://projecteuler.net/problem=15
Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 2020 grid?
做出来一个时间复杂度超高的程序:
1 const int MAX_ROW=20; 2 const int MAX_COLUMN=20; 3 long long path=0; 4 void tree(int i, int j) 5 { 6 if(i==MAX_COLUMN && j==MAX_ROW) 7 { 8 path++; 9 return; 10 } 11 else 12 { 13 if(i!=MAX_COLUMN) 14 tree(i+1, j); 15 if(j!=MAX_ROW) 16 tree(i, j+1); 17 } 18 } 19 20 void p15() 21 { 22 tree(0,0); 23 cout<<path<<endl; 24 }
算17*17以下的都是秒出;
19*19用了不到1分钟
20*20用了大约10分钟才算出来。
递归效率真是低呀。还得优化算法。