-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
161 lines (147 loc) · 4.82 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<iostream>
using namespace std;
#include <string>
#include<fstream>
#include <vector>
void openFile(ifstream &inFile, string file_name);
void processFile(ifstream &inFile_grid, ifstream &inFile_words, vector<string> &grid,
vector<string> &words, string line_grid, string line_words);
void check(vector<string> grid, Trie trie, int start_i, int start_j, int i, int j,
vector<pair<pair<int, int>, pair<int, int>>> &moves);
class Trie{
class Node{
public:
Node* children[26];
bool isEndOfWord;
Node(){
isEndOfWord = false;
for(int i=0; i<26; i++)
children[i] = NULL;
}
void setEndFalse(){
isEndOfWord = false;
}
};
public: Node* root;
public:
Trie(){
root = new Node();
}
void insert(string word){
Node* curr = root;
for(int i=0; i<word.length(); i++){
int index = word[i] - 'a';
if(!curr->children[index])
curr->children[index] = new Node();
curr = curr->children[index];
}
curr->isEndOfWord = true;
//cout<<word<<" is inserted"<<endl;
}
bool search (string word){
Node* curr = root;
for(int i=0; i<word.length(); i++){
int index = word[i] - 'a';
if(!curr->children[index])
return false;
curr = curr->children[index];
}
return curr->isEndOfWord;
}
bool startsWith(string word){
Node* curr = root;
for(int i=0; i<word.length(); i++){
int index = word[i] - 'a';
if(!curr->children[index])
return false;
curr = curr->children[index];
}
return true;
}
void deleteWord(string word){
Node* curr = root;
for(int i=0; i<word.length(); i++){
int index = word[i] - 'a';
if(!curr->children[index])
return;
curr = curr->children[index];
}
curr->isEndOfWord = false;
}
bool isChild(char ch){
int index = ch - 'a';
if(root->children[index]) return true;
else return false;
}
};
int main(){
vector<string> grid;
vector<string> words;
string line_grid ="";
string line_words="";
// Creating file streams to read grid.txt and words.txt files
ifstream inFile_grid;
ifstream inFile_words;
//opening files
openFile(inFile_grid, "grid.txt");
openFile(inFile_words, "words.txt");
// Reading files
processFile(inFile_grid, inFile_words, words, grid, line_grid, line_words);
// creating object of class Trie
Trie trie;
// Inserting all words in the trie
for(auto it: words){
trie.insert(it);
}
// Vector to store the starting and ending coordinates of each word
vector<pair<pair<int, int>, pair<int, int>>>moves;
// For traversing all 8 directions
int arrx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int arry[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// Checking each cell in the grid
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[0].size();j++){
char ch = grid[i][j];
// If it is present in the child of trie's root then continue checking
if(trie.isChild(ch)){
for(int k=0;k<8;k++){
int new_x = i + arrx[k];
int new_y = j + arry[k];
check(grid, trie, i, j, new_x, new_y, moves);
}
}
}
}
}
// All the functions used are below:
void openFile(ifstream &inFile, string file_name){
inFile.open(file_name);
if(!inFile.is_open()){
cout<<"Error! Failed to open file" <<endl;
exit(-1);
}
//else cout<<"File opened";
}
void processFile(ifstream &inFile_grid, ifstream &inFile_words, vector<string> &grid,
vector<string> &words, string line_grid, string line_words){
while(getline(inFile_grid, line_grid) && getline(inFile_grid, line_words)){
grid.push_back(line_grid);
words.push_back(line_words);
}
}
void check(vector<string> grid, Trie trie, int start_i, int start_j, int i, int j,
vector<pair<pair<int, int>, pair<int, int>>> &moves){
int n = grid.size();
int m = grid[0].size();
string substring = "";
start_i = i; start_j = j;
auto node = trie.root;
while(i>=0 && j>=0 && i<n && j<m && node->children[grid[i][j]]!=NULL){
substring += grid[i][j];
node = node->children[grid[i][j]];
if(node->isEndOfWord){
moves.push_back({{start_i, start_j}, {i, j}});
node->setEndFalse();
}
}
}