Digital triangle
Description:
There is a triangle composed of non-negative integers. There is only one number in the first row, and there is no number in the lower left and lower right except for the downlink.
Problem:
Starting from the number in the first row, you can take a grid to the lower left or lower right of each row until the row goes to the bottom, and add all the numbers that pass along the way. How can we make it as big as possible?
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel + 9rLfzsrM4qO6w7 + kernel/kernel + 9PDu9jL3beox/Oz9sv509C1xL/JxNy1xMK3z9 + kernel + 8K3z9/kernel + Kernel X9Lku6/kernel + wsci9z8/kernel + cda-vcd4kpha + PGJyPgo8L3A + CjxwPjxzdHJvbmc + 1 + 7PyM/kernel + kernel = "brush: java;"> # include "stdio. h "# define maxn 100int a [maxn] [maxn], n; inline max (int x, int y) {return x> y? X: y;} // implement int d (int x, int y) {return a [x] [y] + (x = n? 0: max (d (x + 1, y), d (x + 1, y + 1);} int main () {while (~ Scanf ("% d", & n) {int I, j; for (I = 1; I <= n; I ++) {for (j = 1; j <= I; j ++) scanf ("% d", & a [I] [j]);} printf ("max: % d \ n ", d (1, 1);} return 0 ;}Although this is correct, the time efficiency is too low because of repeated computation.
For example, in the following calculation, d (3, 2) is repeatedly called.
The Calculation of d () calls --> d (), d)
The Calculation of d () calls --> d (), d)
Recursive Implementation:
# Include "stdio. h" # define maxn 100int a [maxn] [maxn], n; inline max (int x, int y) {return x> y? X: y;} // implement int d (int x, int y) {int d [n] [n], I, j; for (j = 1; j <= n; j ++) d [n] [j] = a [n] [j]; for (I = n-1; I> = 1; I --) {for (j = 1; j <= I; j ++) d [I] [j] = a [I] [j] + max (d [I + 1] [j], d [I + 1] [j + 1]);} return d [x] [y];} int main () {while (~ Scanf ("% d", & n) {int I, j; for (I = 1; I <= n; I ++) {for (j = 1; j <= I; j ++) scanf ("% d", & a [I] [j]);} printf ("max: % d \ n ", d (1, 1);} return 0 ;}
Implementation of memory-based search:
# Include "stdio. h "# include" string. h "# define maxn 100int a [maxn] [maxn], n; int d [maxn] [maxn]; // The inline max (int x, int y) {return x> y? X: y;}/* Memory words search. The program is divided into two parts. First memset (d,-1, sizeof (d); Initialize all d to-1, and then write a recursive function: */int distance (int I, int j) {if (d [I] [j]> = 0) return d [I] [j]; return d [I] [j] = a [I] [j] + (I = n? 0: max (distance (I + 1, j), distance (I + 1, j + 1);}/* The above program is still recursive, however, the calculation result is also saved in array d. Each number in the question is non-negative. Therefore, if a d [I] [j] has been calculated, it should be non-negative, by initializing all d to-1, you can determine whether d [I] [j]> = 0 has been computed. */Int main () {while (~ Scanf ("% d", & n) {int I, j; for (I = 1; I <= n; I ++) {for (j = 1; j <= I; j ++) scanf ("% d", & a [I] [j]);} memset (d,-1, sizeof (d )); // initialize printf ("max: % d \ n", distance ();} return 0 ;}