-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_2.cpp
69 lines (58 loc) · 1.5 KB
/
code_2.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
//
// code_2.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <string>
#include <iostream>
using namespace std;
#define M 3
#define N 3
string dictionary[] = { "GEEKS", "FOR", "QUIZ", "GO" };
int n = sizeof(dictionary) / sizeof(dictionary[0]);
bool isWord(string& str) {
for (int i = 0; i < n; i++) {
if (str.compare(dictionary[i]) == 0) {
return true;
}
}
return false;
}
void findWordsUtil(char boggle[M][N], bool visited[M][N], int i, int j, string& str) {
visited[i][j] = true;
str = str + boggle[i][j];
if (isWord(str)) {
cout << str << endl;
}
for (int row = i - 1; row <= i + 1 && row < M; row++) {
for (int col = j - 1; col <= j + 1 && col < N; col++) {
if (row >= 0 && col >= 0 && !visited[row][col]) {
findWordsUtil(boggle, visited, row, col, str);
}
}
}
str.erase(str.length() - 1);
visited[i][j] = false;
}
void findWords(char boggle[M][N]) {
bool visited[M][N];
memset(visited,false,sizeof(visited));
string str = "";
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++ ) {
findWordsUtil(boggle, visited, i, j, str);
}
}
}
int main() {
char boggle[M][N] = {
{ 'G', 'I', 'Z' },
{ 'U', 'E', 'K' },
{ 'Q', 'S', 'E' }
};
cout << "\nWords of dictionary\n";
findWords(boggle);
return 0;
}