C ++算法:根据特定条件从2D矩阵中选取n个数字

时间:2021-12-09 12:05:13

I have a 2D matrix of size [3][x] filled with numbers. I want to pick say x numbers from this matrix based on the condition

我有一个大小为[3] [x]的二维矩阵,里面有数字。我想根据条件从这个矩阵中选出x个数字

  1. exactly one number from each column.
  2. 每列只有一个数字。

  3. up to a Max of 'm' numbers from each row (total of all the 3 rows should be x numbers and 3m > x)
  4. 每行最多'm'个数字(所有3行的总数应为x个数字,3m> x)

I want to find the least possible sum of these selected x numbers.

我想找到这些所选x数的最小可能总和。

I was able to pick the numbers based on iterative approach of finding the 'x' small numbers based on above conditions from the matrix. But my answer is not optimal. E.g.:

我能够根据基于上述条件从矩阵中找到'x'小数的迭代方法来选择数字。但我的回答并非最佳。例如。:

5 9 . . . . 
6 15 . . . .
7 19 . . . .

Lets say 5 is picked up initially(so 6 and 7 cannot be picked now). Later on we try to pick 9 but if m elements of row(0) are over we will have to pick 15. Now our solution will be 5+15 = 20 but we could have used 6+9 = 15 as optimal solution.

让我们说5最初被选中(因此现在不能选择6和7)。稍后我们尝试选择9但是如果行(0)的m个元素结束,我们将不得不选择15.现在我们的解决方案将是5 + 15 = 20但我们可以使用6 + 9 = 15作为最佳解决方案。

I am trying to optimize my solution and looking for better algorithms. Can someone provide me some good idea for optimal solution?

我正在尝试优化我的解决方案并寻找更好的算法。有人能为我提供最佳解决方案的好主意吗?

1 个解决方案

#1


0  

The problem reminds me of this one: http://projecteuler.net/problem=345

这个问题让我想起了这个问题:http://projecteuler.net/problem=345

The Hungarian algorithm might work: http://en.wikipedia.org/wiki/Hungarian_algorithm

匈牙利算法可能有效:http://en.wikipedia.org/wiki/Hungarian_algorithm

#1


0  

The problem reminds me of this one: http://projecteuler.net/problem=345

这个问题让我想起了这个问题:http://projecteuler.net/problem=345

The Hungarian algorithm might work: http://en.wikipedia.org/wiki/Hungarian_algorithm

匈牙利算法可能有效:http://en.wikipedia.org/wiki/Hungarian_algorithm