心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
以下是我的代码:
#include<stdio.h>
#include
<string.h>
#define maxn 57
long test,m,n,k;
char r[maxn][maxn];
const long xd[]={-1,-1,0,1,1,1,0,-1},yd[]={0,1,1,1,0,-1,-1,-1};
void search(const char *a,long &x,long &y)
{
    
long pos,xx,yy;
    
for(long i=1;i<=m;i++)
    
for(long j=1;j<=n;j++)
      
if(r[i][j]==a[0])
      {
         
for(long t=0;t<8;t++)
         {
            pos
=0;xx=i;yy=j;
            
while(a[pos]&&a[pos]==r[xx][yy])
            {xx
+=xd[t];yy+=yd[t];pos++;}
            
if(a[pos]==0)
            {x
=i;y=j;return;}
         }
      }
}
int main()
{
    scanf(
"%ld",&test);
    
for(long cas=1;cas<=test;cas++)
    {
       
long x,y;
       
char word[maxn];
       
if(cas>=2) printf("\n");
       memset(r,
0,sizeof(r));
       scanf(
"%ld%ld",&m,&n);
       
for(long i=1;i<=m;i++)
       {
          getchar();
          
for(long j=1;j<=n;j++)
          {
             scanf(
"%c",&r[i][j]);
             
if(r[i][j]>='a'&&r[i][j]<='z') r[i][j]+='A'-'a';
          }
       }
       scanf(
"%ld",&k);
       
for(long i=1;i<=k;i++)
       {
          scanf(
"%s",word);
          
long j=0;
          
while(word[j])
          {
             
if(word[j]>='a'&&word[j]<='z') word[j]+='A'-'a';
             j
++;
          }
          search(word,x,y);
          printf(
"%ld %ld\n",x,y);
       }
    }
return 0;
}


posted on 2010-03-28 14:59 lee1r 阅读(1370) 评论(12)  编辑 收藏 引用 所属分类: 题目分类:字符串处理

FeedBack:
# re: UVa 10010 Where's Waldorf?
2010-07-21 14:10 | 博士眼镜
为什么还要判断A[pos]==0 ?  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-07-21 14:15 | Lee1R
@博士眼镜
能说一下题目意思吗?很长时间了有点忘记了。  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-07-27 15:38 | Kinglee
写的很不错,尤其是那个增量的变化!!!我就觉的会有简单的方法,我为了判断8个方向,写了8个函数!!可就是没想出来!呵呵,楼主的方法很不错,学习了  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-08-02 11:23 | PLC
memset(r,0,sizeof(r));
不用被memory.h包括!水  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-08-22 11:05 | kgduu
while(a[pos]&&a[pos]==r[xx][yy])
{xx+=xd[t];yy+=yd[t];pos++;}
这不需要对xx和yy边界检测吗?  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-08-22 19:30 | Lee1R
@PLC
memset()在string.h里面啊,和memory.h有什么关系?  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-08-22 19:31 | Lee1R
@kgduu
如果越界的话,a[pos]==r[xx][yy]一定不成立。因此,相当于检验了。  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-09-08 09:17 | kgduu
@Lee1R
比如说,当xx < 0时,r[xx][yy]应该会提示内存不可读之类的错误吧
  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-09-08 22:11 | Lee1R
@kgduu
xx不可能小于0的,因为已经有memset(r,0,sizeof(r)),所以xx==0时,比对一定不会成功,会跳出循环。  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2010-09-09 09:33 | kgduu
@Lee1R
哦, 知道了,你是从下标1开始的,不是从下标0开始的  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2011-04-22 21:23 | sai
别一上来就贴代码。讲一下思路好过贴代码....  回复  更多评论
  
# re: UVa 10010 Where's Waldorf?
2011-04-22 22:59 | lee1r
@sai
枚举字符串第一个字符的位置,然后向8个方向检测。  回复  更多评论
  

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