MasterMind
InterviewTime limit1sMemory limit128 MB
Given up to 100 guesses with exact and wrong-position counts, find the smallest four-digit secret consistent with all of them, or NONE.
- Level
Medium4 of 10
- Topics
- Brute force, Implementation, Simulation, Hash map
- Solved
- No attempts yet
Problem
MasterMind is a classic two-player game.
One player is the codemaker: she picks a four-digit secret number with . The other player is the codebreaker, who repeatedly guesses four-digit numbers until she cracks the code.
For every codebreaker guess (with ), the codemaker replies with two integers:
- (): how many digits of the guess are correct and in their correct position in the secret number.
- (): how many of the remaining digits (those not counted by ) are correct but in the wrong position.
For example, if the secret number is and the codebreaker guesses , the feedback is 2 1: the digits and are in their correct positions, and is present but in the wrong position. As another example, in a five-digit variant, if the secret is and the guess is , the feedback is 2 2.
You are given () guesses together with their feedback from the middle of a game. Output the smallest four-digit number (in the range ) that could still be the codemaker's secret, i.e. that is consistent with every guess and its feedback. If no such number exists, output NONE.
Input
- Line 1: a single integer .
- Lines 2 to : line contains guess and its two responses as three space-separated integers , , and .
Output
- A single line with the smallest four-digit number (in the same range as the secret, ) that could be the secret code. If there is no such number, output a single line containing the word
NONE.