1014: The Matrix
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() | stdin/stdout | 3s | 8192K | 1888 | 572 | Standard |
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;
}