标题: euler project第18题求解 [打印本页] 作者: sequencing 时间: 2012-06-11 09:49 标题: euler project第18题求解 欧拉计划第18题(http://projecteuler.net/problem=18)
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
请教大家这类问题暴力破解怎么入手,谢谢各位!作者: yinyuemi 时间: 2012-06-11 10:35 本帖最后由 yinyuemi 于 2012-06-11 10:36 编辑
how about this
$ awk '{for(n=0;n++<NF;)s[NR,n]=a[NR,n]=$n}END{c[0,0]=1;for(n=NR;n>1;n--)for(t=0;++t<n;)if(s[n,t]>s[n,t+1]){c[n-1,t]=t;s[n-1,t]+=s[n,t]}else{c[n-1,t]=t+1;s[n-1,t]+=s[n,t+1]};m=0;for(n=0;n++<NR;){for(p=NR-n;p-->0;)printf " ";for(t=0;t++<n;){if(c[n-1,m]==t)a[n,t]="\033[31m"a[n,t]"\033[0m";printf a[n,t]" "};m=c[n-1,m];print ""}print "max="s[1,1]}' ep18