forked from Razdeep/Placement-Preparation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordboggle.cpp
84 lines (68 loc) · 1.69 KB
/
wordboggle.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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
#define n 3
vector<vector<string> > possible;
void printword(string makewords,string dict[4]){
for(int i=0;i<4;i++){
if(makewords.compare(dict[i])==0){
cout<<makewords<<"\n";
}
}
}
void wordBoggle(char a[n][n],string dict[4],string makewords,
bool visited[n][n],int i,int j){
visited[i][j]=true;
makewords=makewords+a[i][j];
printword(makewords,dict);
// #################
if(j-1>=0 && visited[i][j-1]==false){
wordBoggle(a,dict,makewords,visited,i,j-1);
}
if(j+1<3 && visited[i][j+1]==false){
wordBoggle(a,dict,makewords,visited,i,j+1);
}
// #################
if(i-1>=0 && j-1>=0 && visited[i-1][j-1]==false){
wordBoggle(a,dict,makewords,visited,i-1,j-1);
}
if(i-1>=0 && visited[i-1][j]==false){
wordBoggle(a,dict,makewords,visited,i-1,j);
}
if(i-1>=0 && j+1<3 && visited[i-1][j+1]==false){
wordBoggle(a,dict,makewords,visited,i-1,j+1);
}
// #################
if(i+1<3 && j-1>=0 && visited[i+1][j-1]==false){
wordBoggle(a,dict,makewords,visited,i+1,j-1);
}
if(i+1<3 && visited[i+1][j]==false){
wordBoggle(a,dict,makewords,visited,i+1,j);
}
if(i+1<3 && j+1<3 && visited[i+1][j+1]==false){
wordBoggle(a,dict,makewords,visited,i+1,j+1);
}
if(makewords.length()>=1){
makewords.erase(makewords.length()-1);
}
visited[i][j]=false;
// #################
}
int main()
{
string dict[4]={"GEEKS", "FOR", "QUIZ", "GO"};
// int m=3;
// int n=3;
char a[n][n]={{'G','I','Z'},
{'U','E','K'},
{'Q','S','E'}};
bool visited[n][n] = {{false}};
string makewords="";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
wordBoggle(a,dict,makewords,visited,i,j);
}
}
return 0;
}