Naeioi

量子の風
随笔 - 8, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

UVa 10010

  在一个n*m的字符矩阵中寻找给定的字符串,字符串可以是上下、下上、左右、右左等8个方向。输出每个字符串第一个字符首次出现的位置。
要注意的是换行问题,对一个少一个都判作WA
1 #include <cstdio> 2 #include <string.h> 3 #include <ctype.h> 4 using namespace std; 5 6 const int maxn=60,dx[]= {1,1,1,0,-1,-1,-1,0},dy[]= {-1,0,1,1,1,0,-1,-1}; 7 char G[maxn][maxn]; 8 int N,M,K; 9 10 int main() 11 { 12 #ifndef ONLINE_JUDGE 13 freopen("10010.in","r",stdin); 14 freopen("10010.out","w",stdout); 15 #endif 16 17 int C,t=0; 18 scanf("%d",&C); 19 20 while(C--) { 21 if(t!=0)printf("\\n"); 22 ++t; 23 24 scanf("%d%d",&N,&M); 25 for(int i=1; i<=N; i++) 26 for(int j=1; j<=M; j++) { 27 char tmp; 28 do { 29 tmp=getchar(); 30 } 31 while(!isalpha(tmp)); 32 G[i][j]=tolower(tmp); 33 } 34 35 scanf("%d",&K); 36 getchar(); 37 while(K--) { 38 char str[maxn]; 39 gets(str); 40 int len=strlen(str); 41 42 for(int i=0; i<len; i++) 43 str[i]=tolower(str[i]); 44 45 bool ok=false; 46 for(int i=1; i<=N&&!ok; i++) 47 for(int j=1; j<=M&&!ok; j++) 48 for(int k=0; k<8&&!ok; k++) { 49 int x=i,y=j,count=0; 50 while(x>=1&&y>=1&&x<=N&&y<=M&&count<len&&str[count]==G[x][y]) { 51 ++count; 52 x+=dx[k]; 53 y+=dy[k]; 54 } 55 if(count==len) { 56 ok=true; 57 printf("%d %d\\n",i,j); 58 } 59 } 60 } 61 //printf("\\n"); 62 } 63 64 fclose(stdin); 65 fclose(stdout); 66 return 0; 67 } 68

posted on 2010-11-07 16:36 Naeioi Zhu 阅读(514) 评论(0)  编辑 收藏 引用 所属分类: 题库题解


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理