A complete search problem with a confusing description.
- Take
nInput
sequences and store them in an integer array. We’ll represent each LED configuration by anint
.1
for'Y'
and0
for'N'
. - Now, we start guessing the countdown sequence by determining the first digit. So, we try the initial digits from
9
down-tonInput - 1
. - If the current digit matches the input and the burned-out LED’s –if there’s any–, we move to the previous digit, and so on. For a guess digit to match the input:
- The guess digit should not contradict the burned segments.
- Any difference –between a guess digit and an input– should be an input segment that’s off not vice-versa.
- If we reach the
nInput
th digit, that’s a match. Otherwise, mismatch.
C++, rank 120:
#include<cstdio> using namespace std; int digit[10] = { 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B }; int n, input[16]; bool match; void solve(int inputI, int digitI, int badMask) { // finished if (inputI == n) match = true; // more input else if (!(input[inputI] & badMask)) // matches old burns if (((digit[digitI] ^ input[inputI]) & input[inputI]) == 0) // new burns not revivals solve(inputI + 1, digitI - 1, (input[inputI] ^ digit[digitI]) | badMask); } char ans[2][16] = { "MISMATCH\n", "MATCH\n" }; char line[8], *p; int main() { while (scanf("%d", &n) == 1 && n) { // input for (int i = 0; i < n; ++i) { scanf("%s", p = line); input[i] = 0; do input[i] |= ((*p == 'Y') << (line + 6 - p)); while (*++p); } // solution match = false; for (int digitI = 9; digitI >= n - 1 && !match; --digitI) solve(0, digitI, 0); printf("%s", ans[match]); } }