UVa 10010 Where's Waldorf?
Where's Waldorf? |
Given a m by n grid 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 are to be treated as the same). The matching can be done in any of the eight directions either horizontally, vertically or diagonally through the grid.
Input
The 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 line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input begins with a pair of integers, m followed by n, in decimal notation on a single line. The next m lines contain n letters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be 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).
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by 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 be separated by a single space. The first integer is the line in the grid where the first letter of the given word can be found (1 represents the topmost line in the grid, and m represents the bottommost line). The second integer is the column in the grid where the first letter of the given word can be found (1 represents the leftmost column in the grid, and n represents the rightmost column in the grid). If a word can be found more than once in the grid, then the location which is output should correspond to the uppermost occurence of the word (i.e. the occurence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, the output should correspond to the leftmost of these occurences. All words can be found at least once in the grid.
Sample Input
1 8 11 abcDEFGhigg hEbkWalDork FtyAwaldORm FtsimrLqsrc byoArBeDeyv Klcbqwikomk strEBGadhrb yUiqlxcnBjf 4 Waldorf Bambi Betty Dagbert
Sample Output
2 5 2 3 1 2 7 8
Miguel Revilla
2000-08-22
荒废了半年没怎么写过题了,昨天队里开会,下学期开学初队里还要重新选拨,压力颇大,看来寒假要努力了
这几天复习期末考试,先写几道大水题找找敲代码的感觉吧。。。
这道题是在一个给定的字符矩阵中找匹配的字符串,由于矩阵很小,所以只要八个方向暴力查找即可
WA了两次才过,第一次忘了删freopen,第二次是个非常值得注意的地方,题目中说每两组测试数据之间要有空行,这里注意最后一组测试数据后面是不能有空行的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int m,n; 8 char grid[60][60]; 9 10 int main() 11 { 12 int t; 13 14 scanf("%d",&t); 15 getchar(); 16 getchar(); 17 18 while(t--) 19 { 20 int times; 21 char ask[60]; 22 scanf("%d %d",&m,&n); 23 getchar(); 24 for(int i=1;i<=m;i++) 25 gets(grid[i]); 26 for(int i=1;i<=m;i++) 27 for(int j=0;j<n;j++) 28 if(grid[i][j]>='A'&&grid[i][j]<='Z') 29 grid[i][j]+=32; 30 scanf("%d",×); 31 getchar(); 32 for(int k=1;k<=times;k++) 33 { 34 gets(ask); 35 int len=strlen(ask); 36 for(int j=0;j<len;j++) 37 if(ask[j]>='A'&&ask[j]<='Z') 38 ask[j]+=32; 39 bool not_found=true; 40 for(int i=1;not_found&&i<=m;i++) 41 for(int j=0;not_found&&j<n;j++) 42 { 43 if(grid[i][j]==ask[0]) 44 { 45 int x=i,y=j; 46 int temp=0; 47 while(y<n&&temp<len&&grid[x][y]==ask[temp]) 48 { 49 y++; 50 temp++; 51 } 52 if(temp==len) 53 { 54 not_found=false; 55 printf("%d %d\n",i,j+1); 56 break; 57 } 58 x=i; 59 y=j; 60 temp=0; 61 while(y<n&&x<=m&&temp<len&&grid[x][y]==ask[temp]) 62 { 63 x++; 64 y++; 65 temp++; 66 } 67 if(temp==len) 68 { 69 not_found=false; 70 printf("%d %d\n",i,j+1); 71 break; 72 } 73 x=i; 74 y=j; 75 temp=0; 76 while(x<=m&&temp<len&&grid[x][y]==ask[temp]) 77 { 78 x++; 79 temp++; 80 } 81 if(temp==len) 82 { 83 not_found=false; 84 printf("%d %d\n",i,j+1); 85 break; 86 } 87 x=i; 88 y=j; 89 temp=0; 90 while(x<=m&&y>=0&&temp<len&&grid[x][y]==ask[temp]) 91 { 92 x++; 93 y--; 94 temp++; 95 } 96 if(temp==len) 97 { 98 not_found=false; 99 printf("%d %d\n",i,j+1); 100 break; 101 } 102 x=i; 103 y=j; 104 temp=0; 105 while(y>=0&&temp<len&&grid[x][y]==ask[temp]) 106 { 107 y--; 108 temp++; 109 } 110 if(temp==len) 111 { 112 not_found=false; 113 printf("%d %d\n",i,j+1); 114 break; 115 } 116 x=i; 117 y=j; 118 temp=0; 119 while(y>=0&&x>=1&&temp<len&&grid[x][y]==ask[temp]) 120 { 121 y--; 122 x--; 123 temp++; 124 } 125 if(temp==len) 126 { 127 not_found=false; 128 printf("%d %d\n",i,j+1); 129 break; 130 } 131 x=i; 132 y=j; 133 temp=0; 134 while(x>=1&&temp<len&&grid[x][y]==ask[temp]) 135 { 136 x--; 137 temp++; 138 } 139 if(temp==len) 140 { 141 not_found=false; 142 printf("%d %d\n",i,j+1); 143 break; 144 } 145 x=i; 146 y=j; 147 temp=0; 148 while(x>=1&&y<n&&temp<len&&grid[x][y]==ask[temp]) 149 { 150 x--; 151 y++; 152 temp++; 153 } 154 if(temp==len) 155 { 156 not_found=false; 157 printf("%d %d\n",i,j+1); 158 break; 159 } 160 } 161 162 } 163 } 164 165 if(t) 166 printf("\n"); 167 } 168 169 return 0; 170 }