先说错误,8个方向一开始漏了2个,之后是不知道UVA不支持双反斜杠注释,带注释交上直接CE,之后数据没问题了,找坑,关于格式的问题,跳过去这题也没什么了,下面上题上代码
Where's Waldorf?
Given a m by n grid of letters, ( Where's Waldorf? |
![$1 \leq m,n \leq 20$](https://i-blog.csdnimg.cn/blog_migrate/8d054d4f6a1bc0c58d334f5c437b20e0.gif)
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
18 11
abcDEFGhigg
FtyAwaldORm
FtsimrLqsrc
byoArBeDeyv
Klcbqwikomk
strEBGadhrb
yUiqlxcnBjf
4
Waldorf
Bambi
Betty
Dagbert
Sample Output
2 52 3
1 2
7 8
之后上原装代码,自己留着以后翻翻
#include<stdio.h>
#include<string.h>
#define MAX_SIZE 50 + 10
#define First 0
int found1(char grid[][MAX_SIZE],char word[],int m,int n)//找出单词
{
int L=strlen(word);
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if((word[First]==grid[i][j])&&found2(grid,word,i,j,m,n)==1)
{
printf("%d %d\n",i+1,j+1); /*如果满足条件,输出坐标*/
return ;
}
}
int found2(char grid[][MAX_SIZE],char word[],int a,int b,int m,int n)
{
int i,j,k,w,L=strlen(word);
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)/*对8个方向进行判断*/
{
if(grid[i][j++]!=word[w])
break;
}
if(w==L)
return 1;
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)
{
if(grid[i][j--]!=word[w])
break;
}
if(w==L)
return 1;
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)
{
if(grid[i++][j]!=word[w])
break;
}
if(w==L)
return 1;
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)
{
if(grid[i--][j]!=word[w])
break;
}
if(w==L)
return 1;
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)
{
if(grid[i++][j++]!=word[w])
break;
}
if(w==L)
return 1;
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)
{
if(grid[i--][j--]!=word[w])
break;
}
if(w==L)
return 1;
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)
{
if(grid[i++][j--]!=word[w])
break;
}
if(w==L)
return 1;
for(i=a,j=b,w=0;w<L&&i>=0&&j>=0&&i<m&&j<n;w++)
{
if(grid[i--][j++]!=word[w])
break;
}
if(w==L)
return 1;
else
return 0;
}
int get_grid(char grid[][MAX_SIZE],int m,int n)/*由于不区分大小写,所以统一转化为大写*/
{
int i,j;
for(i=0;i<m;i++)
{
scanf("%s",grid[i]);
strup_r(grid[i]);
}
return ;
}
int get_word(char word[][MAX_SIZE],int n)/*由于不区分大小写,所以统一转化为大写*/
{
int i;
for(i=0;i<n;i++)
{
scanf("%s",word[i]);
strup_r(word[i]);
}
return ;
}
int strup_r(char s[])
{
int L=strlen(s),i;
for(i=0;i<L;i++)
if(s[i]>='a'&&s[i]<='z')
s[i]-=32;
return ;
}
int main()
{
int N,m,n,num,i,j,k,cases;
char grid[MAX_SIZE][MAX_SIZE];
char word[MAX_SIZE][MAX_SIZE];
scanf("%d",&N);
for(cases=1;cases<=N;cases++)
{
scanf("%d%d",&m,&n);
get_grid(grid,m,n); /*输入矩阵*/
scanf("%d",&num);
get_word(word,num); /*输入要提取的单词*/
for(i=0;i<num;i++)
found1(grid,word[i],m,n);
if(cases<N)
printf("\n"); /*这就是坑爹的尾行控制*/
memset(grid,0,sizeof(grid));
memset((word),0,sizeof(word));
} /*初始化一定要做好,养成习惯*/
return 0;
}
总的来说这题还是比较水的,思路很直,就是看你能不能发现UVA给的数据是有多坑爹!!!!