我正在尝试开发一个Java程序来解决https://projecteuler.net/problem=18。但是我遇到了困难,我不确定为什么这段代码不起作用:
int[][] testTriangle = {
{3},
{7, 4},
{2, 4, 6},
{8, 5, 9, 3}
};
HashMap<Integer, Integer> hasGoneDownRoute = new HashMap<Integer, Integer>(); //Stores numbers and start positions to numbers
int largestValue = 0;
int value = 0;
int row = testTriangle.length-1;
for (int startPosition = 0; startPosition <= testTriangle[row].length-1; startPosition++) { //starts from all possible start positions on the bottom row
for (int y = 1; y<=2; y++) { //executes from the same start position twice to get all possible routes (ACTUALLY THIS MIGHT BE WRONG!?)
while (row != 0) { //until it reaches the top row
if (startPosition == 0) { //if it's on the far left
value += testTriangle[row-1][0];
}
else if (startPosition == testTriangle[row].length-1) { //if at's on the far right
value += testTriangle[row-1][testTriangle[row-1].length-1]; //set the value to the row above it on the far right
}
else { //This never gets called?
int noToChooseFrom1 = testTriangle[row-1][startPosition]; //above it and to the right
int noToChooseFrom2 = testTriangle[row-1][startPosition-1]; //above it and to the left
if (hasGoneDownRoute.containsKey(noToChooseFrom1) && hasGoneDownRoute.get(noToChooseFrom1) == startPosition) { //checks if it has gone down a certain route before
value += noToChooseFrom2;
hasGoneDownRoute.put(testTriangle[row-1][startPosition-1], startPosition);
}
else {
value += noToChooseFrom1;
hasGoneDownRoute.put(noToChooseFrom1, startPosition);
}
}
row--;
}
}
if (value > largestValue) {
largestValue = value;
}
System.out.println(largestValue);
}
我刚添加了笔记,试图解释我的思维过程
答案 0 :(得分:0)
你真正需要的是递归。它允许你以for循环不会的方式向前看。 (很容易反正)
创建一个功能
public int findLargestPath(int[][] triangle, row , col) {
// Check to see if the row and column are actually in the triangle
if(col == -1 || col >= triangle[row].length)
{
return -1; //If they are not. ensure that this path wont be taken.
}
if(row == triangle.length -1)
{
return triangle[row][col]; //Return the value.
}
else
{
//return this value plus the maximum path below
return triangle[row][col]+Math.max(findLargestPath(row+1,col),
findLargestPath(row+1,col+1));
}
}
然后在主内部它将是一个简单的
int[][] testTriangle = {
{3},
{7, 4},
{2, 4, 6},
{8, 5, 9, 3}
};
System.out.println("The Maximum Path = "+findLargestPath(testTriangle,0,0));
递归是一种跟踪所有路径而无需创建新变量的方法。我没有在我面前有一个IDE。因此可能存在一些错误。如果你想让我解决它。让我知道,什么时候回到IDE会对这一切进行测试。
答案 1 :(得分:0)
https://projecteuler.net/problem=18
动态编程方法:
导入java.io。*;
公共类主要{
public static void main(String[] args) {
int tri[][] = {
{
6,
0,
0
},
{
2,
-3,
0
},
{
9,
20,
-1
}
};
System.out.println(maxPathSum(tri));
}
static int maxPathSum(int tri[][]) {
for (int i = tri.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
//second condition in conditional ensures that we don't add
//negative numbers, which would only decrease our max sum
if (tri[i][j] > tri[i][j + 1] && tri[i][j] + tri[i - 1][j] > tri[i - 1][j]) {
tri[i - 1][j] += tri[i][j];
} else if (tri[i][j] < tri[i][j + 1] && [i][j + 1] + tri[i - 1][j] > tri[i - 1][j]) {
tri[i - 1][j] += tri[i][j + 1];
}
}
}
return tri[0][0];
}