-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathword boggle.cpp
177 lines (118 loc) Β· 3.59 KB
/
word boggle.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
//////////////////////////////////////////////////////
//Question/Info
Word Boggle
Medium Accuracy: 48.98% Submissions: 11931 Points: 4
Given a dictionary of distinct words and an M x N board where every cell has one character. Find all possible words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example 1:
Input:
N = 1
dictionary = {"CAT"}
R = 3, C = 3
board = {{C,A,P},{A,N,D},{T,I,E}}
Output:
CAT
Explanation:
C A P
A N D
T I E
Words we got is denoted using same color.
Example 2:
Input:
N = 4
dictionary = {"GEEKS","FOR","QUIZ","GO"}
R = 3, C = 3
board = {{G,I,Z},{U,E,K},{Q,S,E}}
Output:
GEEKS QUIZ
Explanation:
G I Z
U E K
Q S E
Words we got is denoted using same color.
Your task:
You donβt need to read input or print anything. Your task is to complete the function wordBoggle() which takes the dictionary contaning N space-separated strings and R*C board as input parameters and returns a list of words that exist on the board.
Expected Time Complexity: O(N*W + R*C^2)
Expected Auxiliary Space: O(N*W + R*C)
Constraints:
1 β€ N β€ 15
1 β€ R, C β€ 50
1 β€ length of Word β€ 60
Company Tags
Amazon Directi Facebook Google MakeMyTrip Microsoft Nvidia Yahoo
author: srj_v
//////////////////////////////////////////////////////
*/
#include <bits/stdc++.h>
using namespace std;
#define _IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long double ld;
typedef long long int lli;
#pragma GCC optimize("Ofast")
void c_p_c()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////////////////////
int32_t main() {
///////////
// c_p_c();
///////////
_IOS
//////////
// code
/*
int t ; cin >> t; while(t--){}
*/
class Solution {
public:
bool dfs(vector<vector<char> >& board, string word, int i , int j, int ind, vector<vector<int>> &vis) {
if (ind == word.size()) return true;
if (i >= 0 and i<board.size() and j >= 0 and j < board[0].size() and vis[i][j] == 0 and board[i][j] == word[ind] )
// so seeing if the cell is unvisited and ... the given char is present in the adjacent cells...
{ vis[i][j] = 1;
if (
dfs(board, word, i + 1, j, ind + 1, vis) or
dfs(board, word, i - 1, j, ind + 1, vis) or
dfs(board, word, i, j + 1, ind + 1, vis) or
dfs(board, word, i, j - 1, ind + 1, vis) or
dfs(board, word, i + 1, j + 1, ind + 1, vis) or
dfs(board, word, i + 1, j - 1, ind + 1, vis) or
dfs(board, word, i - 1, j - 1, ind + 1, vis) or
dfs(board, word, i - 1, j + 1, ind + 1, vis)
) return true;
vis[i][j] = 0 ; // backtrack...
}
return false;
}
bool findd(vector<vector<char> >& board, string word) {
int n = board.size();
int m = board[0].size();
vector<vector<int>> vis(n, vector<int>(m, 0));
int ind = 0;
for (int i = 0 ; i < n; i++) {
for (int j = 0 ; j < m; j++) {
if (board[i][j] == word[ind]) // matching the first char of the word...throughout the matrix...
{
if (dfs(board, word, i, j, ind, vis)) return true;
}
}
}
return false;
}
vector<string> wordBoggle(vector<vector<char> >& board, vector<string>& dictionary) {
// Code here
vector<string> ans;
for (auto it : dictionary) {
if (findd(board, it)) ans.push_back(it);
}
return ans;
}
};
// cerr << "time: " << clock() << " ms" << '\n';
return 0;
}
//////////////////////////////////////////////////////