欧拉计划 第十八题

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.

37 42 4 68 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

7595 6417 47 8218 35 87 1020 04 82 47 6519 01 23 75 03 3488 02 77 73 07 63 6799 65 04 28 06 16 70 9241 41 26 56 83 40 80 70 3341 48 72 33 47 32 37 16 94 2953 71 44 65 25 43 91 52 97 51 1470 11 33 28 77 73 17 78 39 68 17 5791 71 52 38 17 14 91 43 58 50 27 29 4863 66 04 68 89 53 67 30 73 16 69 87 40 3104 62 98 27 23 09 70 98 73 93 38 53 60 04 23

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)

通过从下面三角形的顶部开始并移动到下面一行的相邻数字,从上到下的最大总数为23。

3 7 42 4 68 5 9 3

也就是说,3 + 7 + 4 + 9 = 23。

找到下面三角形从上到下的最大总数:

75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 32 37 16 94 29 53 71 44 65 25 43 91 52 97 51 14 70 11 33 28 77 73 17 78 39 68 17 57 91 71 52 38 17 14 91 43 58 50 27 29 48 63 66 04 68 89 53 67 30 73 16 69 87 40 31 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

注意:由于只有16384条路线,因此可以通过尝试每条路线来解决此问题。然而,问题67,与包含一百行的三角形是同样的挑战; 它无法通过蛮力解决,需要一种聪明的方法!; O)

思路1

暴力求解法:递归搜索每一个节点,递归搜索下一行最大权值的路径;


#include <stdio.h>

#define max 105

int m[max + 5][max + 5] = {0};

int dfs(int x, int y, int n){
	if(x == n) return 0;
	int ans1 = dfs(x + 1, y, n);
	int ans2 = dfs(x + 1, y + 1, n);
	return = (ans1 > ans2 ? ans1 : ans2) + m[x][y];
}

int main(){
	for(int i = 0; i < 15; i++){
		for(int j = 0; j <= i; j++){
			scanf("%d ",&m[i][j]);
		}
	}
	printf("%d\n",dfs(0,0,15));
	return 0;
}

思路2

1.递归搜索:对于每一个节点,递归搜索下一行最大权值的路径;

2.记忆化:在递归的过程中,记住之前走过的路线放在keep中。

#include <stdio.h>

#define max 105

int m[max + 5][max + 5] = {0};
int keep[max + 5][max + 5] = {0};

int dfs(int x, int y, int n){
	if(x == n) return 0;
	if(keep[x][y]) return keep[x][y];
	int ans1 = dfs(x + 1, y, n);
	int ans2 = dfs(x + 1, y + 1, n);
	keep[x][y] = (ans1 > ans2 ? ans1 : ans2) + m[x][y];
	return keep[x][y];
}

int main(){
	for(int i = 0; i < 15; i++){
		for(int j = 0; j <= i; j++){
			scanf("%d ",&m[i][j]);
		}
	}
	printf("%d\n",dfs(0,0,15));
	return 0;
}

思路3

1.动态规划,由于第15层的值取决于第14层的值,所以在每个第14层的位置上找到权值最大的15层值,加到第14层上,依次向上动态更新累加找到权值最大的点;

2.第一层就是累加的最大权值结果。

#include <stdio.h>

#define max 105

int m[max][max] = {0};
int f[max][max] = {0};

int main(){
	for(int i = 0; i < 15; i++){
		for(int j = 0; j <= i; j++){
			scanf("%d", &m[i][j]);
		}
	}
	for(int i = 0; i < 15; i++){
		f[14][i] = m[14][i];
	}
	for(int i = 13; i >= 0; i--){
		for(int j = 0; j <= i; j++){
			f[i][j] = m[i][j];
			if(f[i + 1][j] > f[i + 1][j + 1]){
				f[i][j] += f[i + 1][j];
			}else{
				f[i][j] += f[i + 1][j + 1];
			}
		}
	}
	printf("%d\n",f[0][0]);
	return 0;
}

思路4

从上往下动态规划,下一行的路径由上一行决定,不断更新f的值找到上一行最大的路径累加

#include <stdio.h>

#define max 105

int m[max + 5][max + 5] = {0};
int f[max + 5][max + 5] = {0};

int main(){
	for(int i = 0; i < 15; i++){
		for(int j = 0; j <= i; j++){
			scanf("%d ", &m[i][j]);//将15层的序列传入到数组中;
		}
	}
	f[0][0] = m[0][0];//初始化f[0][0]为第0层的值;
	for(int i = 1; i < 15; i++){//更新之后14层的权值;
		for(int j = 0; j <= i; j++){//每层数字为i+1个;
			if(j != 0 && f[i - 1][j - 1] > f[i - 1][j]){
				f[i][j] = f[i - 1][j - 1];//注意边界条件,每一层的上一层j的值可能为0,超界;
			}
			if(j != i && f[i - 1][j] > f[i - 1][j - 1]){
				f[i][j] = f[i - 1][j];//注意边界条件,每一层的上一层没有第i个值,超界;
			}
			f[i][j] += m[i][j];//不断更新;
		}
	}
	int ans = 0;
	for(int i = 0; i < 16; i++){
		if(f[14][i] > ans) ans = f[14][i];
	}//循环找到最后一层最大的权值
	printf("%d\n", ans);
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_38362049/article/details/80960374