-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word Boggle
61 lines (58 loc) · 1.46 KB
/
Word Boggle
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
class Solution {
public:
bool solve(vector<vector<char>>&b,int x,int y, int pos, string str,int r,int c)
{
int dx[]={1,1,0,-1,-1,-1,0,1};
int dy[]={0,-1,-1,-1,0,1,1,1};
if(pos==str.length())
return true;
for(int i=0;i<8;i++)
{
int nx =x+dx[i];
int ny=y+dy[i];
if(nx<r&&nx>=0&&ny<c&&ny>=0&&b[nx][ny]==str[pos])
{
char ch=b[nx][ny];
b[nx][ny]='#';
if(solve(b,nx,ny,pos+1,str,r,c)==true)
{
b[nx][ny]=ch;
return true;
}
b[nx][ny]=ch;
}
}
return false;
}
vector<string> wordBoggle(vector<vector<char> >& b, vector<string>& d) {
int r=b.size();
int c=b[0].size();
vector<string>ans;
for(auto itr:d)
{
bool flag=false;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(b[i][j]==itr[0])
{
char ch=b[i][j];
b[i][j]='#';
if(solve(b,i,j,1,itr,r,c)==true)
{
b[i][j]=ch;
ans.push_back(itr);
flag=true;
break;
}
b[i][j]=ch;
}
}
if(flag)
break;
}
}
return ans;
}
};