Problem 59586. List ways to make a sum in Killer Sudoku

In Sudoku, the subject of several Cody problems, one tries to arrange the digits 1 through 9 in a 9x9 grid of 81 cells (or a 3x3 arrangement of nine 3x3 boxes) so that every row, column, and 3x3 box contains each digit exactly once. In the variant Killer Sudoku, digits are placed in cages that indicate the sum of the digits; digits cannot be repeated in cages. In the example below from Wikipedia, the cages are indicated by colors.
Solving Killer Sudoku with few (or no) given digits requires ready ideas of how to make particular sums. A two-digit cage with a sum of 3 can contain only 1 and 2, and a two-digit cage with a sum of 4 can contain only 1 and 3 because 2 cannot be repeated. A three-digit cage with a sum of 21 can have some arrangement of [9 8 4], [9 7 5], or [8 7 6]. The three-digit cage summing to 17 in the central box of the example has seven possible sets, but if the box already has an 8 outside the 17-cage, then only four of those sets are possible: [9 7 1], [9 6 2], [9 5 3], [7 6 4].
Write a function to list the ways to make a sum with a given number of digits. If only two arguments are given, then all of the digits 1-9 are possible. If a third argument is given, then it indicates the digits that cannot be used. The output should be a matrix in which the digits decrease along the rows and the rows are sorted in decreasing order of the first digit, then the second digit, then the third, etc., as in the examples. List only one arrangement: in the last example above, only [9 7 1] is given, not [9 1 7], [7 9 1], [7 1 9], [1 9 7], and [1 7 9]. If the sum cannot be made, return [].

Solution Stats

80.0% Correct | 20.0% Incorrect
Last Solution submitted on Jan 29, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers3

Suggested Problems

More from this Author265

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!