Under construction

Still being built. Things may change

Barcode of Judgment

Time limit1sMemory limit128 MB

Summary
A barcode is a fixed 7x9 grid pattern placed somewhere in a binary image, possibly rotated; find all valid placements and decode the data bits, or report none or ambiguity.
Level

Hard8 of 10

Topics
String, Implementation, Brute force, Simulation
Solved
No attempts yet

Problem

While stuck in traffic, I spot an ad on a nearby taxi for an intriguing restaurant. The ad carries an embedded 2-D barcode, so I photograph it with my phone; the phone reads the code and hands me a web address with a map to the nearest location.[1]

Given an "image" of a 2-D barcode, determine the text string it encodes.

A 2-D barcode has both fixed and variable parts. In the diagram below, fixed cells are drawn as 0 or 1, and variable (data) cells are drawn as -:

1010---01
1000---00
10-------
10-------
10-------
1000---00
101000001

The variable cells hold the data. When the barcode is in the orientation shown above (7 rows by 9 columns, i.e. 30 data cells), the data cells are read left-to-right, top-to-bottom.

For example, given this barcode:

101000101
100011000
100100010
101101011
100111011
100011000
101000001

the data bits, read in order, are:

001110010001011010110111011110

Split them into 5-bit chunks:

00111 00100 01011 01011 01110 11110

Convert each chunk to its decimal value:

7 4 11 11 14 30

Finally, decode each value with this table:

ValueCharacter
0–25lowercase letters a through z
26_ (underscore)
27/ (forward slash)
28: (colon)
29. (period)
30! (exclamation point)
31? (question mark)

So this barcode decodes to hello!. Every barcode encodes exactly 6 characters (30 data bits ÷ 5 = 6).

Input

The first line contains an integer NN (1≤N≤1001 \le N \le 100): the number of data sets.

Each data set begins with a line H W, where HH (7≤H≤807 \le H \le 80) and WW (9≤W≤809 \le W \le 80) are the height and width of the image. The next HH lines each contain WW binary digits (0 or 1) representing the black-and-white image your camera captured.

Any barcode that appears in the image was photographed square-on, but it may have been rotated by any multiple of 90 degrees.

Output

For each data set, print one line:

  • If the image contains exactly one valid barcode, print its decoded value.
  • If the image contains no valid barcode, print NOCODE.
  • If the image contains more than one valid barcode — so the genuine one cannot be identified — print INTERFERENCE.

Reference

[1] Rekimoto, Jun and Ayatsuka, Yuji. 2000. CyberCode: Designing Augmented Reality Environments with Visual Tags. Sony Computer Science Laboratories, Inc.

Examples4

  1. Example 1

    Input
    3
    10 10
    0000000000
    0101000101
    0100011000
    0100100010
    0101101011
    0100111011
    0100011000
    0101000001
    0000000000
    0000000000
    10 10
    0000000000
    1010001010
    1000110000
    1001000100
    1011010100
    1001110100
    1000110000
    1010000010
    0000000000
    0011111111
    10 10
    0000000000
    0101000101
    0100011000
    0100100010
    0001101011
    0100111011
    0100011000
    0101000001
    0000000000
    0000000000
    
    Expected output
    hello!
    INTERFERENCE
    NOCODE
    
  2. Example 2

    Input
    1
    7 9
    101000001
    100010000
    100111011
    101011101
    101001000
    100011100
    101000001
    
    Expected output
    cho.sh
    
  3. Example 3

    Input
    1
    8 12
    000000000000
    000000000000
    000000000000
    000000000000
    000000000000
    000000000000
    000000000000
    000000000000
    
    Expected output
    NOCODE
    
  4. Example 4

    Input
    1
    9 22
    0000000000000000000000
    0101000001001010001010
    0100000000001000100000
    0100001000001001110100
    0101000011001000010010
    0100010000001001010010
    0100010100001000011000
    0101000001001010000010
    0000000000000000000000
    
    Expected output
    INTERFERENCE