Under construction

Still being built. Things may change

Word Puzzle

Interview

Time limit1sMemory limit128 MB

Summary
Given a letter grid and a list of words, find each word's starting cell and one of eight reading directions, choosing the earliest match by row, column, then direction.
Level

Medium4 of 10

Topics
Array, Brute force, Simulation, String matching
Solved
No attempts yet

Problem

Solve a word puzzle: find several words hidden in a rectangular grid.

Each word appears along one of the 8 directions (up, down, left, right, and the four diagonals) as a run of consecutive cells. For each word, determine the cell where it starts and the direction in which it is read from that cell.

The top-left cell has coordinates (0,0)(0, 0). The row number increases downward and the column number increases to the right.

Directions are labeled A through H, starting from North and going clockwise.

LetterDirectionRow stepColumn step
ANorth−1-100
BNortheast−1-1+1+1
CEast00+1+1
DSoutheast+1+1+1+1
ESouth+1+100
FSouthwest+1+1−1-1
GWest00−1-1
HNorthwest−1-1−1-1

Input

The first line contains the number of rows LL, the number of columns CC, and the number of words to find WW (0<L,C,W≤10000 < L, C, W \le 1000).

The next LL lines describe the grid; each line consists of CC uppercase letters.

The following WW lines each contain one word to find. Every word consists of uppercase letters and is guaranteed to appear in the grid.

Output

Print the answer for each word on its own line, over WW lines. Each line contains the starting cell's row number, its column number, and a single letter (A~H) giving the reading direction, separated by spaces.

A word may appear in more than one place. In that case, apply the following rules in order and print only the single earliest match:

  • smaller row number first;
  • if the row numbers are equal, smaller column number;
  • if both the row and column numbers are equal, smaller direction letter (A is smallest, H is largest).

Examples2

  1. Example 1

    Input
    20 20 10
    QWSPILAATIRAGRAMYKEI
    AGTRCLQAXLPOIJLFVBUQ
    TQTKAZXVMRWALEMAPKCW
    LIEACNKAZXKPOTPIZCEO
    FGKLSTCBTROPICALBLBC
    JEWHJEEWSMLPOEKORORA
    LUPQWRNJOAAGJKMUSJAE
    KRQEIOLOAOQPRTVILCBZ
    QOPUCAJSPPOUTMTSLPSF
    LPOUYTRFGMMLKIUISXSW
    WAHCPOIYTGAKLMNAHBVA
    EIAKHPLBGSMCLOGNGJML
    LDTIKENVCSWQAZUAOEAL
    HOPLPGEJKMNUTIIORMNC
    LOIUFTGSQACAXMOPBEIO
    QOASDHOPEPNBUYUYOBXB
    IONIAELOJHSWASMOUTRK
    HPOIYTJPLNAQWDRIBITG
    LPOINUYMRTEMPTMLMNBO
    PAFCOPLHAVAIANALBPFS
    MARGARITA
    ALEMA
    BARBECUE
    TROPICAL
    SUPREMA
    LOUISIANA
    CHEESEHAM
    EUROPA
    HAVAIANA
    CAMPONESA
    
    Expected output
    0 15 G
    2 11 C
    7 18 A
    4 8 C
    16 13 B
    4 15 E
    10 3 D
    5 1 E
    19 7 C
    11 11 H
    
  2. Example 2

    Input
    3 3 8
    ABC
    DEF
    GHI
    EB
    EC
    EF
    EI
    EH
    EG
    ED
    EA
    
    Expected output
    1 1 A
    1 1 B
    1 1 C
    1 1 D
    1 1 E
    1 1 F
    1 1 G
    1 1 H