|
|
SPOJ Problem Set (classical)
413. Word Puzzles
Problem code: WPUZZLES
|
Word puzzles are usually simple and very entertaining for all ages.
They are so entertaining that Pizza-Hut company started using table
covers with word puzzles printed on them, possibly with the intent to
minimise their client's perception of any possible delay in bringing
them their order.
Even though word puzzles may be entertaining to solve by hand, they
may become boring when they get very large. Computers do not yet get
bored in solving tasks, therefore we thought you could devise a
program to speedup (hopefully!) solution finding in such puzzles.
The following figure illustrates the PizzaHut puzzle. The names of the
pizzas to be found in the puzzle are: MARGARITA, ALEMA,
BARBECUE, TROPICAL, SUPREMA, LOUISIANA, CHEESEHAM, EUROPA, HAVAIANA,
CAMPONESA.
|
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
|
0 |
Q |
W |
S |
P |
I |
L |
A |
A |
T |
I |
R |
A |
G |
R |
A |
M |
Y |
K |
E |
I |
|
1 |
A |
G |
T |
R |
C |
L |
Q |
A |
X |
L |
P |
O |
I |
J |
L |
F |
V |
B |
U |
Q |
|
2 |
T |
Q |
T |
K |
A |
Z |
X |
V |
M |
R |
W |
A |
L |
E |
M |
A |
P |
K |
C |
W |
|
3 |
L |
I |
E |
A |
C |
N |
K |
A |
Z |
X |
K |
P |
O |
T |
P |
I |
Z |
C |
E |
O |
|
4 |
F |
G |
K |
L |
S |
T |
C |
B |
T |
R |
O |
P |
I |
C |
A |
L |
B |
L |
B |
C |
|
5 |
J |
E |
W |
H |
J |
E |
E |
W |
S |
M |
L |
P |
O |
E |
K |
O |
R |
O |
R |
A |
|
6 |
L |
U |
P |
Q |
W |
R |
N |
J |
O |
A |
A |
G |
J |
K |
M |
U |
S |
J |
A |
E |
|
7 |
K |
R |
Q |
E |
I |
O |
L |
O |
A |
O |
Q |
P |
R |
T |
V |
I |
L |
C |
B |
Z |
|
8 |
Q |
O |
P |
U |
C |
A |
J |
S |
P |
P |
O |
U |
T |
M |
T |
S |
L |
P |
S |
F |
|
9 |
L |
P |
O |
U |
Y |
T |
R |
F |
G |
M |
M |
L |
K |
I |
U |
I |
S |
X |
S |
W |
|
10 |
W |
A |
H |
C |
P |
O |
I |
Y |
T |
G |
A |
K |
L |
M |
N |
A |
H |
B |
V |
A |
|
11 |
E |
I |
A |
K |
H |
P |
L |
B |
G |
S |
M |
C |
L |
O |
G |
N |
G |
J |
M |
L |
|
12 |
L |
D |
T |
I |
K |
E |
N |
V |
C |
S |
W |
Q |
A |
Z |
U |
A |
O |
E |
A |
L |
|
13 |
H |
O |
P |
L |
P |
G |
E |
J |
K |
M |
N |
U |
T |
I |
I |
O |
R |
M |
N |
C |
|
14 |
L |
O |
I |
U |
F |
T |
G |
S |
Q |
A |
C |
A |
X |
M |
O |
P |
B |
E |
I |
O |
|
15 |
Q |
O |
A |
S |
D |
H |
O |
P |
E |
P |
N |
B |
U |
Y |
U |
Y |
O |
B |
X |
B |
|
16 |
I |
O |
N |
I |
A |
E |
L |
O |
J |
H |
S |
W |
A |
S |
M |
O |
U |
T |
R |
K |
|
17 |
H |
P |
O |
I |
Y |
T |
J |
P |
L |
N |
A |
Q |
W |
D |
R |
I |
B |
I |
T |
G |
|
18 |
L |
P |
O |
I |
N |
U |
Y |
M |
R |
T |
E |
M |
P |
T |
M |
L |
M |
N |
B |
O |
|
19 |
P |
A |
F |
C |
O |
P |
L |
H |
A |
V |
A |
I |
A |
N |
A |
L |
B |
P |
F |
S |
Problem
Your task is to produce a program that given the word puzzle and words
to be found in the puzzle, determines, for each word, the position of
the first letter and its orientation in the puzzle.
You can assume that the left upper corner of the puzzle is the origin,
(0,0). Furthemore, the orientation of the word is marked clockwise
starting with letter A for north (note: there are 8 possible
directions in total).
Input
The first line of the input contains a number T ≤ 10 which indicates the number of test cases to follow.
Each test case starts with a line consisting of three positive numbers: The
number of lines of the word puzzle, 0 < L ≤ 1000, the number of columns,
0 < C ≤ 1000, and the number of words to be found,
0 < W ≤ 1000. The following L input lines, each
consisting of C uppercase letters, contain
the word puzzle. Then at last the W words are input one per line. You can assume that each word can be found exactly once in the word puzzle.
Output
For each test case your program should output W lines: For each word (using the same order as the
words were input) print a triplet defining the coordinates, line and column,
where the first letter of the word appears, followed by a letter
indicating the orientation of the word according to the rules defined
above. Each value in the triplet must be separated by one space only.
Print one blank line between test cases.
Example
Input:
1
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
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
| Added by: | Adrian Kuegel |
| Date: | 2005-08-10 |
| Time limit: | 15s
|
| Source limit: | 50000B |
| Languages: | All except: TECS |
| Resource: | ACM Southwestern European Regional Contest 2002 |
|
|
|
|