【发布时间】:2020-06-14 13:22:02
【问题描述】:
我在理解下面的代码时遇到了问题。我用//comment标记了我不理解的部分
函数'search()'被递归调用。 MaxRemaining[] 数组有 15 个元素,Size 是一个值为 15 的 const。
const unsigned int Size = 15;
unsigned short matrix[Size][Size] =
{
{ 7, 53, 183, 439, 863, 497, 383, 563, 79, 973, 287, 63, 343, 169, 583 },
{ 627, 343, 773, 959, 943, 767, 473, 103, 699, 303, 957, 703, 583, 639, 913 },
{ 447, 283, 463, 29, 23, 487, 463, 993, 119, 883, 327, 493, 423, 159, 743 },
{ 217, 623, 3, 399, 853, 407, 103, 983, 89, 463, 290, 516, 212, 462, 350 },
{ 960, 376, 682, 962, 300, 780, 486, 502, 912, 800, 250, 346, 172, 812, 350 },
{ 870, 456, 192, 162, 593, 473, 915, 45, 989, 873, 823, 965, 425, 329, 803 },
{ 973, 965, 905, 919, 133, 673, 665, 235, 509, 613, 673, 815, 165, 992, 326 },
{ 322, 148, 972, 962, 286, 255, 941, 541, 265, 323, 925, 281, 601, 95, 973 },
{ 445, 721, 11, 525, 473, 65, 511, 164, 138, 672, 18, 428, 154, 448, 848 },
{ 414, 456, 310, 312, 798, 104, 566, 520, 302, 248, 694, 976, 430, 392, 198 },
{ 184, 829, 373, 181, 631, 101, 969, 613, 840, 740, 778, 458, 284, 760, 390 },
{ 821, 461, 843, 513, 17, 901, 711, 993, 293, 157, 274, 94, 192, 156, 574 },
{ 34, 124, 4, 878, 450, 476, 712, 914, 838, 669, 875, 299, 823, 329, 699 },
{ 815, 559, 813, 459, 522, 788, 168, 586, 966, 232, 308, 833, 251, 631, 107 },
{ 813, 883, 451, 509, 615, 77, 281, 613, 459, 205, 380, 274, 302, 35, 805 }
};
unsigned int maxRemaining[Size];
unsigned int search(unsigned int row = 0, unsigned int columnMask = 0,unsigned int sum = 0, unsigned int atLeast = 0){
if (row == Size)
return sum;
if (sum + maxRemaining[row] <= atLeast) //explain this line
return 0;
for (unsigned int column = 0; column < Size; column++)
{
auto mask = 1 << column; //explain this line
if ((columnMask & mask) != 0) //explain this line
continue;
auto current = search(row + 1, columnMask | mask, sum + matrix[row][column], atLeast); //explain whats with the 2nd parameter.
if (atLeast < current)
atLeast = current;
}
return atLeast;
}
【问题讨论】:
-
你的问题到底是什么?
-
那些**的呢?
-
如果你不明白一些代码是如何工作的,最好的办法是用你的调试器简单地执行它,一次一行,并观察它的逻辑,一步一步.你有没有试过在调试器中运行这个程序,如果没有,为什么不呢?
-
是C++还是你不懂的算法?我们可以在这里帮助您了解 C++ 含义和语法,但可能无法帮助您解决特定问题的算法。
-
我已经编辑了 cmets 并尝试重新格式化。请立即查看
标签: c++ recursion dynamic-programming