-
Notifications
You must be signed in to change notification settings - Fork 0
/
boggleSolver.cpp
151 lines (123 loc) · 3.41 KB
/
boggleSolver.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
#include "boggleSolver.h"
bool f (std::string i,std::string j) { return (i.size()>j.size()); }
BoggleSolver::BoggleSolver(const std::string& g)
{
setGrid(g);
}
void BoggleSolver::setGrid(const std::string& g)
{
std::vector<char> tmp;
for(std::string::const_iterator it = g.begin(); it != g.end();++it)
{
if(*it != ' ')
{
if( *it != ',')
tmp.push_back((char)toupper(*it));
else
{
grid.push_back(tmp);
tmp.clear();
}
}
}
grid.push_back(tmp);
if(!checkGridIntegrity())
{
std::cerr << "Invalid Grid" << std::endl;
resetGrid();
}
}
std::vector< std::string > BoggleSolver::giveMeWords() const
{
int x=0,y=0;
std::set< std::string > result;
std::set< std::string > otherResult;
std::vector<std::string> res;
for(std::vector<std::vector<char> >::const_iterator it = grid.begin(); it != grid.end();++it)
{
x=0;
for(std::vector<char>::const_iterator itChar = (*it).begin(); itChar != (*it).end();++itChar)
{
Coordinates c = Coordinates(x,y);
std::set< Coordinates > alreadyUsed;
alreadyUsed.clear();
otherResult = giveMeWords("",c,alreadyUsed);
result.insert( otherResult.begin(), otherResult.end() );
x++;
}
y++;
}
res.insert( res.end(), result.begin(), result.end() );
std::sort(res.begin(), res.end(),f);
return res;
}
std::set< std::string > BoggleSolver::giveMeWords(std::string base, Coordinates start, std::set< Coordinates > alreadyUsed) const
{
std::set< std::string > result;
std::set< std::string > otherResult;
base+=getLetter(start.getX(),start.getY());
alreadyUsed.insert(start);
if(base.size()>=MIN_WORD_LENGTH )
{
//Attention, upper != lower+1
std::string upperBound = dictionnary.getUpperBound(base);
if(dictionnary.isValid(base))
result.insert(base);
//Optimisation : si on ne trouve pas base dans le début de upper bound, on peut arreter
if(upperBound.compare(0,base.size(),base)!=0)
return result;
}
std::vector<Coordinates> adj = start.getAdjacent(gridSize-1,gridSize-1);
for(std::vector<Coordinates >::const_iterator it = adj.begin(); it != adj.end();++it)
{
if(alreadyUsed.find(*it)==alreadyUsed.end())
{
otherResult = giveMeWords(base,*it,alreadyUsed);
std::set_union(result.begin(), result.end(), otherResult.begin(), otherResult.end(), std::inserter(result,result.end()));
}
}
return result;
}
char BoggleSolver::getLetter(int x, int y) const
{
return grid.at(y).at(x);
}
bool BoggleSolver::checkGridIntegrity()
{
int col = -1;
gridSize = 0;
for(std::vector<std::vector<char> >::const_iterator it = grid.begin(); it != grid.end();++it)
{
if(col==-1)
col = (*it).size();
else if((*it).size()!=col)
return false;
}
if(col!=grid.size())
return false;
gridSize = col;
return true;
}
void BoggleSolver::resetGrid()
{
for(std::vector<std::vector<char> >::iterator it = grid.begin(); it != grid.end();++it)
{
(*it).clear();
}
grid.clear();
}
void BoggleSolver::displayGrid() const
{
for(std::vector<std::vector<char> >::const_iterator it = grid.begin(); it != grid.end();++it)
{
for(std::vector<char>::const_iterator itChar = (*it).begin(); itChar != (*it).end();++itChar)
{
std::cout << *itChar << " ";
}
std::cout << std::endl;
}
}
void BoggleSolver::setDictionnary(const Dictionnary& dico)
{
dictionnary = dico;
}