Guessing Game II
Time limit2sMemory limit128 MB
Given up to 8 guess/response pairs, decide whether the 4-digit secret is already unique, find the smallest next guess that identifies it, or report that none can.
- Level
Medium7 of 10
- Topics
- Brute force, Hash map, Implementation, Math
- Solved
- No attempts yet
Problem
The secret to a gate is a 4-digit code, written with leading zeros so that it always has exactly four digits (from 0000 to 9999).
Someone has been making guesses at the code. Each guess is a 4-digit code, and the gate answers with two numbers:
- circles — the number of positions at which the guessed digit equals the secret digit in that same position;
- squares — the number of remaining guessed digits that occur in the secret but at a different position, counted with multiplicity. Formally, for each digit value let ; then .
You are given up to 8 recorded guess/response pairs for one secret. Using only these pairs, decide which of the following holds:
- The pairs already pin down the secret uniquely.
- The secret is not yet unique, but there exists one more guess whose response would tell it apart from every remaining possibility.
- Even one more guess cannot always reveal the secret.
A single guess uniquely identifies the secret when, over all codes still consistent with the recorded pairs, that guess produces a different response for every one of them — so whatever response the gate gives, exactly one candidate remains.
For example, suppose the recorded pairs are 5888 → 3 circles, 0 squares, 1234 → 0 circles, 0 squares, and 4567 → 0 circles, 0 squares. Only three codes are consistent: 0888, 8888, and 9888. Guessing 0009 next separates them (their responses are 1 circle, 0 circles, and 0 circles, 1 square), so 0009 identifies the secret. Guessing 0888 would not, because 8888 and 9888 would both answer 3 circles, 0 squares. Several guesses work here (0009, 0889, 0898, …); output the numerically smallest, 0009.
Input
The first line contains the number of test cases ().
Each of the next lines describes one test case: it starts with the number of recorded guess/response pairs (), followed by integers. Each group of three integers is a triple guess, circles, squares.
Each guess is given as an integer and is implicitly padded on the left with zeros to four digits — for example the guess 1 means 0001. Tokens on a line may be separated by any amount of whitespace.
Output
For each test case, print exactly one line:
- if the recorded pairs already determine the secret, print
The secret is: SSSS, whereSSSSis the secret as four digits; - otherwise, if some further guess can uniquely identify the secret, print
There is a guess that can uniquely identify the secret: GGGG, whereGGGGis that guess as four digits; if several guesses qualify, use the numerically smallest; - otherwise, print
There is no guess that can uniquely identify the secret.