1014: The Matrix

 1014: The Matrix


StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
stdin/stdout3s8192K1888572Standard

Given a matrix of characters, and a list of words, output whether or not each word is present in the matrix. Words may appear forwards and backwards. They may appear horizontally, vertically, and diagonally.

Input


The matrix, followed by a list of words. Only lower case characters will be used. Each matrix will be square, and contain no more than 20 characters on a side. The string XXX denotes the end of input.

Output


For each word, report whether or not it is present in the matrix. If it is present, output should read “<word> is in the matrix.” If it is not present, output should read “<word> is not in the matrix.”, where <word> is the word in question.

Sample Input

applexy
pxlhjke
edeqgfl
xocgvpl
gghnmnn
tahuupu
qdgbywb
apple
axe
apex
cat
car
hat
computer
gum
XXX

Sample Output

apple is in the matrix.
axe is in the matrix.
apex is in the matrix.
cat is not in the matrix.
car is not in the matrix.
hat is in the matrix.
computer is not in the matrix.
gum is in the matrix.
#include<stdio.h>
#include<string.h>
int main()
{
 char s[30][30];
 scanf("%s",s[0]);
 int len=strlen(s[0]);
 int i;
 for(i=1;i<len;i++)
  scanf("%s",s[i]);
 char c[30];
 //8个方向
 while(scanf("%s",c)!=-1)
 {
  if(strcmp(c,"XXX")==0) break;
  printf("%s",c);
  int f=0,k=0;
  int i,j;
  for(i=0;i<len;i++)
  {
   for(j=0;j<len;j++)
   {
    if(s[i][j]==c[0])
    {
     for(k=0;k<strlen(c);k++)
     {
      //上(i>k)
      if(i<k) break;
      if(c[k]!=s[i-k][j]) break;
      if(k==strlen(c)-1)  f=1;
     }
     for(k=0;k<strlen(c);k++)
     {
      //右上
      if(i<k||j+k>=len) break;
      if(c[k]!=s[i-k][j+k]) break;
      if(k==strlen(c)-1) f=1;
     }
     for(k=0;k<strlen(c);k++)
     {
      //右
      if(j+k>=len) break;
      if(c[k]!=s[i][j+k]) break;
      if(k==strlen(c)-1) f=1;
     }
     for(k=0;k<strlen(c);k++)
     {
      //右下
      if(j+k>=len||i+k>=len) break;
      if(c[k]!=s[i+k][j+k]) break;
      if(k==strlen(c)-1) f=1;
     }
     for(k=0;k<strlen(c);k++)
     {
      //下
      if(i+k>=len) break;
      if(c[k]!=s[i+k][j]) break;
      if(k==strlen(c)-1) f=1;
     }
     for(k=0;k<strlen(c);k++)
     {
      //左下
      if(j<k||i+k>=len) break;
      if(c[k]!=s[i+k][j-k]) break;
      if(k==strlen(c)-1) f=1;
     }
     for(k=0;k<strlen(c);k++)
     {
      //左
      if(j<k) break;
      if(c[k]!=s[i][j-k]) break;
      if(k==strlen(c)-1) f=1;
     }
     for(k=0;k<strlen(c);k++)
     {
      //左上
      if(j<k||i<k) break;
      if(c[k]!=s[i-k][j-k]) break;
      if(k==strlen(c)-1) f=1;
     }
    }
   }
  }
  if(f)
   printf(" is in the matrix./n");
  else
   printf(" is not in the matrix./n");
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值