Where's Waldorf? -uva手记
来源:互联网 发布:如何修改电脑mac地址 编辑:程序博客网 时间:2024/05/18 13:30
先说错误,8个方向一开始漏了2个,之后是不知道UVA不支持双反斜杠注释,带注释交上直接CE,之后数据没问题了,找坑,关于格式的问题,跳过去这题也没什么了,下面上题上代码
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
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 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给的数据是有多坑爹!!!!
- Where's Waldorf? -uva手记
- uva Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- uva 10010 Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- uva-10010 - Where's Waldorf?
- UVA 10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVA 10010 Where's Waldorf?
- UVa 10010: Where's Waldorf?
- UVA 10010 Where's Waldorf?
- UVa - 10010 - Where's Waldorf?
- UVA 10010 Where's Waldorf?
- uva 10010 - Where's Waldorf?
- uva 10010 Where's Waldorf?
- uva - 10010 - Where's Waldorf?
- Android中ListView分页加载数据
- 计算机软件考试各资格介绍
- Linux Ubuntu 打开.exe文件
- JAVA支持数组每维长度不同
- OSD的主要实现方法和类型
- Where's Waldorf? -uva手记
- 解决mysql 1040错误Too many connections的方法
- 当更换cocos2d-x版本时,删除VS2010下面的cocos2d-x模板方法 VA默认路径
- java调用oracle存储过程的自定义类型(可变数组)。
- 用代码给 core data property 设置默认值
- 关于高精度问题的自我总结
- PHP类的静态方法和静态变量
- 手机号码验证,邮箱,车牌验证
- zoj-1789