FreeCell
Time limit1sMemory limit256 MB
Decide whether a sorted pile of K cards can be moved using N free cells and M empty stacks.
- Level
Easy3 of 10
- Topics
- Math
- Solved
- No attempts yet
Problem
FreeCell solitaire is mostly about sorting, just like computer science. The goal is to sort a deck of cards under a fairly convoluted set of rules.
The rules are these. A shuffled deck is laid out in eight stacks. Only the topmost card of a stack may be moved. A card of value may be moved onto another stack only if that stack is empty, or if its topmost card has the opposite color and value . For instance, the three of hearts may be moved on top of the four of clubs. You also have four free cells, empty at the start. Each free cell holds at most one card, and that card moves under the same rules as a stack card.
Figure H.1 shows a few legal moves.

Figure H.1: legal moves for the three of hearts and the six of clubs
Most FreeCell computer games let you move a pile of cards at once, provided there are enough empty cells and empty stacks to reach the same position by moving the cards one at a time. In the figure above, the whole pile with 5, 4 and 3 can be moved onto the six of spades by parking 3 and 4 in free cells and on the empty stack.
You want to move a sorted pile of cards from one stack onto the top of another stack. The destination stack is not empty, and its topmost card has the right color and value. Ignore every other non-empty stack, because otherwise you would have to account for the color and value of the topmost card of each one, and that gets too complicated.
Even with those stacks ignored, you still have free cells and empty stacks. Is the move possible?
Input
The input holds several test cases. Each test case is one line with three integers , and separated by spaces. is the number of free cells, is the number of empty stacks, and is the size of the pile you are moving. This is a generalized form of FreeCell, so and . The input runs to the end of file.
Output
For each test case, print yes on its own line if a pile of cards can be moved using free cells and empty stacks, and no otherwise.