Tecle & Some
Time limit2sMemory limit512 MB
List every way to split S into terms of at most D digits whose concatenated digits form a path on a phone keypad using each digit at most once.
- Level
Medium7 of 10
- Topics
- DFS, Backtracking, Implementation
- Solved
- No attempts yet
Problem
Strike Boy, as his nickname suggests, is a boy obsessed with every kind of computer game. He is spending his vacation on a paradise island where computers are not allowed. He had fun for a while with the games on his cell phone, but the battery ran out and the island has no electricity, so he had to stop playing. Strike Boy then invented a new pastime that uses his phone's keypad. In this two-player game, one player chooses two integers and . The opponent must then find a sequence of terms such that:
- Every term of the sequence is a number with decimal digits, except the last term, which may have between and digits.
- The sum of all terms of the sequence is .
- The digits that form the terms correspond to the keys of a standard cell phone keypad ('0' to '9').
- Each digit is used at most once in the whole sequence.
- The first term may start with any digit, but when the digits of the sequence are read from left to right, each key is always an immediate neighbor of the previously used key (vertically, horizontally, or diagonally). This also holds across the boundary between two terms.
The keypad layout is shown below. The 0 key is directly below 8, so the neighbors of 0 are 7, 8, and 9.
1 2 3
4 5 6
7 8 9
0
A term is written as the exact string of digits it uses. Any term may therefore start with 0 (for example 074), and so may the last term (for example, with , a last term 07).
For example, if and , there are only two solutions that obey the rules: [074, 156] and [085, 142, 3]. The sequence [230] is not a solution because key '3' is not a neighbor of key '0'.

Help Strike Boy check whether his opponent's answers are correct: write a program that, given and , prints every possible solution.
Input
The input contains several test cases. Each test case is a single line with two integers and separated by one space: the desired sum and the number of digits of each term (, ).
The end of the input is marked by a line with . Do not process that line.
Output
For each test case, print one answer. The first line of an answer is the test case identifier in the format #i, where i starts at 1 and increases by one for each test case.
If a solution exists, print every possible sequence, one per line. Separate the terms of a sequence with a single space, and print each term exactly as the digits it uses, including leading zeros. If there is no solution, print a line containing only the word impossivel.
Print the sequences in increasing lexicographic order. Sequence precedes sequence if and only if is non-empty and one of the following holds:
- is the empty sequence.
- .
- and the sequence precedes the sequence .
Here two terms and are compared as digit strings, not as numeric values: character by character from the left, and if one string is a prefix of the other, the shorter one comes first. For example, the term 08 precedes the term 8, and the term 012 precedes the term 3. This order is the same as comparing, in the same way, the digit strings obtained by concatenating the terms of each sequence without spaces.