每个单元格中都有一个点矩阵,该矩阵如何使用两次遍历从该网格中获取最大点。
有一些条件要满足-
第一次遍历从网格的左上角单元格开始,并且应该到达左下角。在第二次遍历中,从右上角到右下角
我们只能从一个单元格移到当前单元格的底部,左下角和当前单元格的右下角。
如果一个遍历已经从一个单元格中获得了一些点,则在下一次遍历中将不会从该单元格中获得任何点。
Input: A grid with points. 3 6 8 2 5 2 4 3 1 1 20 10 1 1 20 10 1 1 20 10 Output: Maximum points collected by two traversals is 73. From the first traversal, it gains: 3 + 2 + 20 + 1 + 1 = 27 From the second traversal, it gains: 2 + 4 + 10 + 20 + 10 = 46
findMaxVal(mTable, x, y1, y2)
输入-一个3D数组作为存储表,x值和y1,y2。
输出-最大值。
Begin if x, y1 and y2 is not valid, then return - ∞ if both traversal is complete, then if y1 = y2, then return grid[x, y1] else return grid[x, y1] + grid[x, y2] if both traversal are at last row, then return - ∞ if subProblem is solved, then return mTable[x, y1, y2] set res := - ∞ if y1 = y2, then temp := grid[x, y1] else temp := grid[x, y1] + grid[x, y2] res := max of res and (temp + findMaxVal(mTable, x+1, y1, y2-1)) res := max of res and (temp + findMaxVal(mTable, x+1, y1, y2+1)) res := max of res and (temp + findMaxVal(mTable, x+1, y1, y2)) res := max of res and (temp + findMaxVal(mTable, x+1, y1-1, y2)) res := max of res and (temp + findMaxVal(mTable, x+1, y1-1, y2-1)) res := max of res and (temp + findMaxVal(mTable, x+1, y1-1, y2+1)) res := max of res and (temp + findMaxVal(mTable, x+1, y1+1, y2)) res := max of res and (temp + findMaxVal(mTable, x+1, y1+1, y2-1)) res := max of res and (temp + findMaxVal(mTable, x+1, y1+1, y2+1)) return true if mTable[x, y1, y2] = res End
#include<iostream> #define ROW 5 #define COL 4 using namespace std; int grid[ROW][COL] = { {3, 6, 8, 2}, {5, 2, 4, 3}, {1, 1, 20, 10}, {1, 1, 20, 10}, {1, 1, 20, 10}, }; bool isValidInput(int x, int y1, int y2) { return (x >= 0 && x < ROW && y1 >=0 && y1 < COL && y2 >=0 && y2 < COL); } int max(int a, int b) { return (a>b)?a:b; } int findMaxVal(int mTable[ROW][COL][COL], int x, int y1, int y2) { if (!isValidInput(x, y1, y2)) //when in invalid cell, return -ve infinity return INT_MIN; if (x == ROW-1 && y1 == 0 && y2 == COL-1) //when both traversal is complete return (y1 == y2)? grid[x][y1]: grid[x][y1] + grid[x][y2]; if (x == ROW-1) //both traversal are at last row but not completed return INT_MIN; if (mTable[x][y1][y2] != -1) //when subproblem is solved return mTable[x][y1][y2]; int answer = INT_MIN; //initially the answer is -ve infinity int temp = (y1 == y2)? grid[x][y1]: grid[x][y1] + grid[x][y2]; //store gain of the current room //找到所有可能值的答案并使用最大值 answer = max(answer, temp + findMaxVal(mTable, x+1, y1, y2-1)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1, y2+1)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1, y2)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1-1, y2)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1-1, y2-1)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1-1, y2+1)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1+1, y2)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1+1, y2-1)); answer = max(answer, temp + findMaxVal(mTable, x+1, y1+1, y2+1)); return (mTable[x][y1][y2] = answer); //store the answer in the mTable and return. } int findMaxCollection() { //创建一个备忘录表并将所有值设置为-1- int mTable[ROW][COL][COL]; for(int i = 0; i<ROW; i++) for(int j = 0; j<COL; j++) for(int k = 0; k<COL; k++) mTable[i][j][k] = -1; return findMaxVal(mTable, 0, 0, COL-1); } int main() { cout << "Maximum collection is " << findMaxCollection(); return 0; }
输出结果
Maximum collection is 73
问题内容: 说我有一个数组。如何一次迭代两个? 问题答案: 您可以使用称为stride(to :, by :)的进度循环,每n个元素对元素进行一次迭代: Xcode 8.3.2•Swift 3.1
我正试图找出如何以有效的方式遍历2.5D网格。栅格本身是二维的,但栅格中的每个单元都有一个最小/最大浮动高度。要遍历的线由两个三维浮点坐标定义。如果进入/退出网格单元之间的z值范围与该单元的最小/最大高度不重叠,我希望停止遍历该线。 我目前正在使用2D DDA算法按顺序遍历网格单元格(见图),但我不确定如何在到达每个网格单元格时计算z值。如果可以的话,我可以在进入/离开单元格时根据单元格的最小/最
问题内容: 我正在尝试创建一个Java程序来清理和合并表中的行。该表很大,大约有50万行,而我当前的解决方案运行得非常慢。我要做的第一件事就是简单地获得一个内存中的对象数组,这些对象表示表的所有行。这是我在做什么: 一次选择说1000行的增量 使用JDBC在以下SQL查询中获取结果集SELECT * FROM TABLE WHERE ID> 0 AND ID <1000 将结果数据添加到内存数组中
层次遍历 给定二叉树的包含虚结点的先序遍历序列信息,按层次顺序给出遍历的结果。 输入格式: 首先输入一个整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据在一行中输入一个字符串(不含空格且长度不超过80),表示二叉树的先序遍历序列(其中@表示虚结点)。 输出格式: 对于每组测试,输出层次遍历的结果。 输入样例: 1 ABD@@EG@@@C@F@@ 输出样例: ABCDEFG 代码长度限制
问题内容: 我试图将两个数组排列在一起,结果总是不正确。我将向您展示我的代码,获得的结果以及正在寻找的结果。 我想我只是做错了,但不确定其他方法。 我的代码: 结果:(缩短以节省空间) 我正在寻找的结果如下: 问题答案: 问题 嗯,问题当然出在您嵌套的foreach循环上。因为对于数组的每个元素,您都循环遍历整个数组(所以总共有* 次迭代)。 解决方案 为了解决这个问题,您必须一次遍历两个数组。
问题内容: 当前在React中,我正在使用一个数组进行迭代。但是,如何使用map同时遍历两个数组? 编辑 像这样,我希望在每次迭代之前都添加图标。我计划将这些图标放在另一个数组中。因此,这就是为什么要迭代两个数组的原因。 问题答案: 如果可能的话,我建议将文本与图像一起存储在对象数组中,例如: 这样,您就可以遍历数组并同时选择两个成员,例如: 如果这不可能,则只需映射一个数组并通过当前索引访问第二
目前,结构是一棵树,但如果有循环,上面的代码是否防止了无休止的循环?如果没有,这又怎能做到呢?
def lookup(root): row = [root] while row: print(row) row = [kid for item in row for kid in (item.left, item.right) if kid]