In a 4x4 grid, it is required that each row and each column contains exactly one selected square. Enumerate all the four squares that are selected. And identify the selection that, among those that meet the aforementioned criteria, yields the maximum sum of the four numbers.
4x4 grid as follows:
Grid[{{11, 21, 31, 40}, {12, 22, 33, 42}, {13, 22, 33, 43}, {15, 24,
34, 44}}, Frame -> All]
numbers = {{11, 21, 31, 40}, {12, 22, 33, 42}, {13, 22, 33, 43}, {15,
24, 34, 44}}
allPermutations = Permutations[Range[1, 4]]
validSelections =
Select[allPermutations,
Module[{selection},
selection =
numbers[[#[[1]]]] + numbers[[#[[2]] + 1]][[#[[2]]]] +
numbers[[#[[3]] + 2]][[#[[3]] + 1]] +
numbers[[#[[4]]]][[#[[4]]];
Table[selection[[#]] == numbers[[i, #]], {i, 4}], {#,
4}] === {1, 1, 1, 1}] &]
The above code did not produce the correct result.
ResourceFunction["MinSumPermutation"][-numbers]
gives{2, 3, 4, 1}
. Try, for example, withnumbers = RandomInteger[{-1000, 1000}, {1000, 1000}]
to check that this is an amazing resource function! $\endgroup$