【发布时间】:2017-06-27 13:02:37
【问题描述】:
问题的另一种描述:Compute a matrix which satisfies certain constraints
给定一个函数,其唯一参数是一个 4x4 矩阵 (int[4][4] matrix),确定该函数的最大可能输出(返回值)。
4x4 矩阵必须满足以下约束:
- 所有条目都是介于 -10 和 10(含)之间的整数。
- 必须是对称矩阵,entry(x,y) = entry(y,x)。
- 对角线条目必须是正数,entry(x,x) > 0。
- 所有 16 个条目的总和必须为 0。
函数必须只对矩阵的值求和,没什么花哨的。
我的问题:
给定这样一个对矩阵的某些值求和的函数(矩阵满足上述约束),我如何找到该函数的最大可能输出/返回值?
例如:
/* The function sums up certain values of the matrix,
a value can be summed up multiple or 0 times. */
// for this example I arbitrarily chose values at (0,0), (1,2), (0,3), (1,1).
int exampleFunction(int[][] matrix) {
int a = matrix[0][0];
int b = matrix[1][2];
int c = matrix[0][3];
int d = matrix[1][1];
return a+b+c+d;
}
/* The result (max output of the above function) is 40,
it can be achieved by the following matrix: */
0. 1. 2. 3.
0. 10 -10 -10 10
1. -10 10 10 -10
2. -10 10 1 -1
3. 10 -10 -1 1
// Another example:
// for this example I arbitrarily chose values at (0,3), (0,1), (0,1), (0,4), ...
int exampleFunction2(int[][] matrix) {
int a = matrix[0][3] + matrix[0][1] + matrix[0][1];
int b = matrix[0][3] + matrix[0][3] + matrix[0][2];
int c = matrix[1][2] + matrix[2][1] + matrix[3][1];
int d = matrix[1][3] + matrix[2][3] + matrix[3][2];
return a+b+c+d;
}
/* The result (max output of the above function) is -4, it can be achieved by
the following matrix: */
0. 1. 2. 3.
0. 1 10 10 -10
1. 10 1 -1 -10
2. 10 -1 1 -1
3. -10 -10 -1 1
我不知道从哪里开始。目前我正在尝试估计满足约束的 4x4 矩阵的数量,如果数量足够小,则可以通过蛮力解决问题。
有没有更通用的方法? 这个问题的解决方案是否可以推广,使其可以很容易地适应给定矩阵上的任意函数和矩阵的任意约束?
【问题讨论】:
-
相对于上述条件,要最大化的值究竟是多少?或者这是问题的一部分?
-
@Codor 函数的返回值被最大化。换句话说:该函数选择要对哪些条目求和,目标是得出一个矩阵,该矩阵的总和最大。
-
请注意,由于对称性,矩阵实际上比任意矩阵“小”;没有 16 个可能不同的条目,但只有 10 个。
-
总结某些值是什么意思?该函数只能选择肯定条目?
-
然后取最大值乘以Integer.MAX_VALUE,问题描述有地方不对。