Where's Waldorf? -uva手记

来源:互联网 发布:如何修改电脑mac地址 编辑:程序博客网 时间:2024/05/18 13:30
昨天晚上无限太烂没刷的起来,这水题我早上爬起来花了大概一个半小时才AC下了,不是这题多么难,是这题数据给的太坑爹外加英语各种看不懂,翻译先自己翻译了半个小时,这尼玛。。之后就是题目给的测试数据,给了一组!!!就是这一组我没看出来他的数据间的空行,还有最后一行的控制,这也是坑,不过刷过了“三角波”那道关于练习空格的“坑爹战斗机”的题以后,这个坑也不算深
先说错误,8个方向一开始漏了2个,之后是不知道UVA不支持双反斜杠注释,带注释交上直接CE,之后数据没问题了,找坑,关于格式的问题,跳过去这题也没什么了,下面上题上代码
 

  Where's Waldorf? 

Given a m by n grid of letters, ( $1 \leq m,n \leq 20$), 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$1 \leqm,n \leq 50$ 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 ( $1 \leq k \leq 20$). 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

之后上原装代码,自己留着以后翻翻

#include<stdio.h>#include<string.h>#define MAX_SIZE 50 + 10#define First 0int 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给的数据是有多坑爹!!!!

0 0
原创粉丝点击