I use the naive string SEARCH method that scans for the eight directions ctions at each point in the matrix. Due to the relatively small input size, this method works well enough to get the top 20% ranking.
Code:
- /*************************************** **********************************
- * Copyright (c) 2008 by liukaipeng *
- * Liukaipeng at gmail dot com *
- **************************************** *********************************/
- /* @ Judge_id 00000 10010 C ++ "Where's Waldorf "*/
- # Include <iostream>
- # Include <fstream>
- # Include <string>
- # Include <vector>
- Using namespace STD;
- Int const wordsize = 52;
- Int const wordcount = 20;
- Void find_words (char text [] [wordsize], int row, int Col,
- Char words [] [wordsize], int nwords,
- Pair <int, int> positions [])
- {
- Bool found [wordcount] = {false };
- Int H [] = {0, 1, 1, 1, 0,-1,-1,-1 };
- Int V [] = {-1,-1, 0, 1, 1, 1, 0,-1 };
- For (INT r = 1; r <= row; ++ R ){
- For (INT c = 1; C <= Col; ++ c ){
- For (int w = 0; W <nwords; ++ W ){
- If (found [w] | words [w] [0]! = Text [r] [c]) continue;
- For (int d = 0; D <8; ++ d ){
- Int I = 1;
- For (INT y = R + V [d], x = C + H [d];
- Words [w] [I]! = '/0' & words [w] [I] = text [y] [x];
- ++ I, Y + = V [d], x + = H [d]);
- If (words [w] [I] = '/0 '){
- Positions [w]. First = R;
- Positions [w]. Second = C;
- Found [w] = true;
- Break;
- }
- }
- }
- }
- }
- }
- Int main (INT argc, char * argv [])
- {
- # Ifndef online_judge
- Filebuf In, out;
- Cin. rdbuf (in. Open (string (argv [0]) + ". In"). c_str (), ios_base: In ));
- Cout. rdbuf (out. Open (string (argv [0]) + ". Out"). c_str (), ios_base: Out ));
- # Endif
- Int ncases;
- Cin> ncases;
- While (ncases --> 0 ){
- Int row, Col;
- Cin> row> Col;
- Cin. Ignore (2048, '/N ');
- Char text [wordsize] [wordsize];
- For (INT r = 1; r <= row; ++ R ){
- Cin. Getline (Text [R] + 1, wordsize-1 );
- For (INT I = 1; text [r] [I]! = '/0'; ++ I ){
- Text [r] [I] = toupper (Text [r] [I]);
- }
- }
- Int nwords;
- Cin> nwords;
- Char words [wordcount] [wordsize];
- Cin. Ignore (2048, '/N ');
- For (int w = 0; W <nwords; ++ W ){
- Cin. Getline (words [w], wordsize );
- For (INT I = 1; words [w] [I]! = '/0'; ++ I ){
- Words [w] [I] = toupper (words [w] [I]);
- }
- }
- Pair <int, int> positions [wordcount];
- Find_words (text, row, Col, words, nwords, positions );
- For (int w = 0; W <nwords; ++ W ){
- Cout <positions [w]. First <''<positions [w]. Second <'/N ';
- }
- If (ncases> 0) cout <'/N ';
- }
- Return 0;
- }