Word Puzzle
InterviewTime limit1sMemory limit128 MB
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 . 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.
Input
The first line contains the number of rows , the number of columns , and the number of words to find ().
The next lines describe the grid; each line consists of uppercase letters.
The following 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 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).