-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_string_in_a_grid.cpp
72 lines (63 loc) · 1.71 KB
/
find_string_in_a_grid.cpp
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
/*
Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid.
A word can be matched in all 8 directions at any point. Word is said be found in a direction
if all characters match in this direction (not in zig-zag form). The 8 directions are, horizontally
left, horizontally right, vertically up, vertically down and 4 diagonal directions.
*/
#include <bits/stdc++.h>
using namespace std;
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vi vector<int>
int x[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int y[] = {1, 1, 1, 0, -1, -1, -1, 0};
bool found(vvc &grid, string &word, int r, int c){
for(int k = 0; k < 8; k++){
bool cur = true;
int i = r, j = c;
for(int idx = 0; idx < word.length(); idx++){
if(i < 0 || j < 0 || i >= grid.size() || j > grid[0].size()){
cur = false;
break;
}
if(grid[i][j] != word[idx]){
cur = false;
break;
}
i += x[k];
j += y[k];
}
if(cur == true) return true;
}
return false;
}
vvi searchWord(vvc grid, string word){
vvi res;
for(int i = 0; i < grid.size(); i++){
for(int j = 0; j < grid[0].size(); j++){
if(found(grid, word, i, j)){
res.push_back({i, j});
}
}
}
return res;
}
int main(){
int m, n;
cin >> m >> n;
vvc grid(m, vector<char>(n));
string word;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cin >> grid[i][j];
}
}
cin >> word;
vvi res = searchWord(grid, word);
for(auto v: res){
for(auto i: v){
cout << i << " ";
} cout << "\n";
}
return 0;
}