forked from ravikartar/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Search a Word in a 2D Grid of characters
79 lines (68 loc) · 1.82 KB
/
Search a Word in a 2D Grid of characters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// C++ programs to search a word in a 2D grid
#include <bits/stdc++.h>
using namespace std;
// For searching in all 8 direction
int x[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int y[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
// This function searches in
// all 8-direction from point
// (row, col) in grid[][]
bool search2D(char *grid, int row, int col,
string word, int R, int C)
{
// If first character of word doesn't
// match with given starting point in grid.
if (*(grid+row*C+col) != word[0])
return false;
int len = word.length();
// Search word in all 8 directions
// starting from (row, col)
for (int dir = 0; dir < 8; dir++) {
// Initialize starting point
// for current direction
int k, rd = row + x[dir], cd = col + y[dir];
// First character is already checked,
// match remaining characters
for (k = 1; k < len; k++) {
// If out of bound break
if (rd >= R || rd < 0 || cd >= C || cd < 0)
break;
// If not matched, break
if (*(grid+rd*C+cd) != word[k])
break;
// Moving in particular direction
rd += x[dir], cd += y[dir];
}
// If all character matched, then value of k must
// be equal to length of word
if (k == len)
return true;
}
return false;
}
// Searches given word in a given
// matrix in all 8 directions
void patternSearch(char *grid, string word,
int R, int C)
{
// Consider every point as starting
// point and search given word
for (int row = 0; row < R; row++)
for (int col = 0; col < C; col++)
if (search2D(grid, row, col, word, R, C))
cout << "pattern found at "
<< row << ", "
<< col << endl;
}
// Driver program
int main()
{
int R = 3, C = 13;
char grid[R][C] = { "GEEKSFORGEEKS",
"GEEKSQUIZGEEK",
"IDEQAPRACTICE" };
patternSearch((char *)grid, "GEEKS", R, C);
cout << endl;
patternSearch((char *)grid, "EEE", R, C);
return 0;
}