#include<bits/stdc++.h>
using namespace std;
int x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int y[] = {-1, 0, 1, -1, 1, -1, 0, 1};
char a[1000][1000];
int row, column;
bool isvalid(int r, int c)
{
return (r < 0) || (r >= row) || (c < 0) || (c >= column);
}
bool check(int r, int c, string word)
{
if(a[r][c] != word[0])
return false;
int i, j;
for(i=0; i<8; i++)
{
int tx = r + x[i];
int ty = c + y[i];
for(j=1; j<word.size(); j++)
{
if(isvalid(tx, ty))
break;
if(a[tx][ty] != word[j])
break;
tx = tx + x[i];
ty = ty + y[i];
}
if(j == word.size())
return true;
}
return false;
}
void PatternSearch(string word)
{
int f = 0;
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
if(check(i, j, word))
{
f = 1;
cout << "Pattern Found At :: (" << i << "," << j << ")\n";
}
}
}
if(f == 0)
cout << "Pattern Not Found." << "\n";
}
int main()
{
string word;
cout << "Enter Row And Column :: ";
cin >> row >> column;
cout << "Enter Array Element :: \n";
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
cin >> a[i][j];
}
}
cout << "Enter Word Which Want to Search :: ";
cin >> word;
cout << "\n";
PatternSearch(word);
return 0;
}
using namespace std;
int x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int y[] = {-1, 0, 1, -1, 1, -1, 0, 1};
char a[1000][1000];
int row, column;
bool isvalid(int r, int c)
{
return (r < 0) || (r >= row) || (c < 0) || (c >= column);
}
bool check(int r, int c, string word)
{
if(a[r][c] != word[0])
return false;
int i, j;
for(i=0; i<8; i++)
{
int tx = r + x[i];
int ty = c + y[i];
for(j=1; j<word.size(); j++)
{
if(isvalid(tx, ty))
break;
if(a[tx][ty] != word[j])
break;
tx = tx + x[i];
ty = ty + y[i];
}
if(j == word.size())
return true;
}
return false;
}
void PatternSearch(string word)
{
int f = 0;
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
if(check(i, j, word))
{
f = 1;
cout << "Pattern Found At :: (" << i << "," << j << ")\n";
}
}
}
if(f == 0)
cout << "Pattern Not Found." << "\n";
}
int main()
{
string word;
cout << "Enter Row And Column :: ";
cin >> row >> column;
cout << "Enter Array Element :: \n";
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
cin >> a[i][j];
}
}
cout << "Enter Word Which Want to Search :: ";
cin >> word;
cout << "\n";
PatternSearch(word);
return 0;
}
/***Example :: Row = 4 and Column = 4
ABCD
EFGH
IJKL
MNOP
Word :: AFKP
Pattern Found at (0, 0)***/
Comments
Post a Comment