Phone Plot
Time limit1sMemory limit128 MB
Classify each phone number as EXCELLENT, GOOD, or BAD by counting the fewest straight segments that cover its keypad trace.
- Level
Medium5 of 10
- Topics
- Geometry, Simulation, Implementation
- Solved
- No attempts yet
Problem
Mobile phones are part of everyday life, and every mobile phone has a number pad used to dial a telephone number. A typical number pad is a 4-by-3 grid of cells, shown in Figure 1. Some phone numbers are easy to memorize because dialing their digits in order traces an easy-to-remember geometric shape.

Figure 1
For a phone number we build a Phone Plot: a sequence of connected line segments. Suppose an -digit phone number is . The first segment connects the center of pad to the center of pad , the second connects the centers of and , and in general the -th segment connects the centers of pad and pad ; the last segment connects and .
Here are two examples. The Phone Plot for 1023289873 is drawn with thick lines in Figure 2(a), and Figure 2(b) shows the Phone Plot for 1159533969.

Figure 2(a)

Figure 2(b)
Note that some segments may overlap other segments along the same straight line.
We characterize a Phone Plot by its Minimal Number of Decomposing Segments (MNDS): the smallest number of straight line segments whose union is exactly the given Phone Plot. For example, the MNDS of 1023289873 is 5 and the MNDS of 1159533969 is 3. If the Phone Plot collapses to a single point (for example 0000000000), its MNDS is defined to be 0.
Using the MNDS, each phone number is sorted into one of three disjoint classes.
- EXCELLENT if the MNDS is at most 3.
- GOOD if the MNDS is exactly 4.
- BAD if the MNDS is 5 or more.
By this rule 1023289873 is BAD and 1159533969 is EXCELLENT. Figure 3 shows one more example, 5233999008; its MNDS is 5, so it is BAD.

Figure 3
For each given phone number, decide whether it is EXCELLENT, GOOD, or BAD according to the MNDS of its Phone Plot.
Input
The first line contains the number of test cases . Each of the next lines contains one phone number, a string of decimal digits. Each phone number has more than 5 and fewer than 20 digits.
Output
For each test case, print EXCELLENT, GOOD, or BAD on its own line, according to the MNDS of that phone number's Phone Plot.