Where ' s Waldorf?Time
limit:MS
Memory Limit:0KB
64bit IO Format:%lld & %llu SubmitStatus
Description
Given am byNgrid of letters, (), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A Word can match the letters in the grid regardless of case (i.e. upper and lower case letters is to be treated as the SA Me). The matching can be do in any of the eight directions either horizontally, vertically or diagonally through the grid.Inputthe input begins with a single positive integer on a line by itself indicating the number of the cases following, each Of them as described below. This was followed by a blank line, and there was also a blank line between the consecutive inputs.
The input begins with a pair of integers, m followed by n, and decimal notation on a. The next m lines contain nletters each; This is the grid of letters in which, the words of the list must be found. The letters in the grid is in upper or lower case. Following the grid of letters, another integer K appears on a line by itself (). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters).
Outputfor each test case, the output must follow the description below. The outputs of the consecutive cases would be separated to a blank line.
For each word in the word list, a pair of integers representing the location of the corresponding word in the grid must be Output. The integers must is separated by a single space. The first integer is the line in the grid where the first letter of the given word can found (1 represents the topmost Line in the grid, andmRepresents the bottommost line). The second integer is the column, the grid where the first letter of the given word can be found (1 represents the LEFTM OST column in the grid, andNRepresents the rightmost column in the grid). If A word can be found more than once in the grid and then the location which are output should correspond to the uppermost OC Curence of the word (i.e. the occurence which places the first letter of the word closest to the top of the grid). If the or more words is uppermost, the output should correspond to the leftmost of these occurences. All words can is found at least once in the grid.
Sample Input
18 11abcdefghigghebkwaldorkftyawaldormftsimrlqsrcbyoarbedeyvklcbqwikomkstrebgadhrbyuiqlxcnbjf4waldorfbambibettydagbert
Sample Output
2 52 31) 27 8
Test instructions: Give the character grid of n rows M column, then give K words, ask to find the word in the character grid, can be found in eight directions. If you can find the complete word, the output coordinates. Word size does not count
Idea: Unlike DFS, this is just a one-way walk in one direction. Just find the first letter of the word, search in these eight directions, and see if it is the word.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include < Iostream>using namespace Std;char str[110][110];int n,m;int x,y;int jx[]= {0,0,1,-1,1,-1,1,-1};int jy[]= {1,-1,0,0,1 , -1,-1,1};void findx (char *a) {int i,j,k; int cnt; int dx,dy; for (i=0;i<n;i++) {for (j=0;j<m;j++) {if (A[0]==str[i][j]) {fo R (k=0;k<8;k++) {dx=i; Dy=j; cnt=0; while (a[cnt]==str[dx][dy]&&a[cnt]) {dx+=jx[k]; DY+=JY[K]; cnt++; } if (a[cnt]==0) {x=i; Y=j; return; }}}}}}int main () {int t,i,j,k,t; Char s[110]; ScaNF ("%d", &t); for (t=1;t<=t;t++) {if (t>=2) printf ("\ n"); scanf ("%d%d", &n,&m); for (i=0;i<n;i++) {scanf ("%s", Str[i]); for (j=0;j<m;j++) if (str[i][j]>= ' A ' &&str[i][j]<= ' Z ') str[i][j]=str[i][j]+32; } scanf ("%d", &k); while (k--) {scanf ("%s", s); For (I=0;i<strlen (s); i++) if (s[i]>= ' A ' &&s[i]<= ' Z ') s[i]=s[i]+32; Findx (s); printf ("%d%d\n", x+1,y+1); }} return 0;}
UVA 10010-where ' s Waldorf? (looking for a string in eight directions)