-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolver.cpp
More file actions
235 lines (213 loc) · 8.43 KB
/
solver.cpp
File metadata and controls
235 lines (213 loc) · 8.43 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <string>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <fstream>
#include <unordered_map>
#include <utility>
using namespace std;
string allletters = "qwertyuiopasdfghjklzxcvbnm";
pair<int,int> solutionPaths[3][10];
unordered_map<char,int> LetterValues;
void recsolve(const pair<char,int> (&values)[5][5], unordered_map<string,bool> &k20, pair<int,int> (&pathtaken)[11], pair<string,int> (&solutions)[3], const pair<int,int> &doublepoints, int i, int j, int depth, bool swap){
//if depth is > 8 then we should NOT run
if(depth > 8){return;}
int newDepth = depth + 1;
//checking the word this makes against what is already in the solutions tab
int chari = 0;
string word = "";
int value = 0;
bool crossesdob = false;
//we add our current character to path taken.
pathtaken[depth-1] = make_pair(i,j);
map<pair<int,int>,bool> havebeen;
while(chari != 11 && pathtaken[chari].first != -1)
{
int pathtakeword = pathtaken[chari].first;
int pathtakevalue = pathtaken[chari].second;
havebeen[make_pair(pathtakeword,pathtakevalue)] = true;
word += values[pathtakeword][pathtakevalue].first;
value += values[pathtakeword][pathtakevalue].second;
if(pathtakevalue == doublepoints.second && pathtakeword == doublepoints.first){crossesdob = true;} //if we cross the double points, double our value
chari++;
}
if(crossesdob){value *= 2;}
if(depth >= 5){value += 10;}
//cout << word << endl;
//cout << solutions[0].first << endl;
//now we have our word and its value. We check it against the solutions such that if it is more valuable than one of them they get written there instead.
if(k20.find(word) != k20.end())
{
for(int soli = 0; soli < 3; soli++) //We only care about top 3
{
if(solutions[soli].second < value)
{
// cout << word << endl;
// cout << value << endl;
solutions[soli] = make_pair(word,value);
for(int w = 0; w < 10; w++)
{
solutionPaths[soli][w] = make_pair(pathtaken[w].first,pathtaken[w].second);
}
break;
}
}
}
else if(swap)
{
//can we make a swap to make a word that k20 stores?
//its only 25*depth number of swaps and checks.
for(int swapinteger = 0; swapinteger < depth; swapinteger++)
{
char tempstorecharacter = word[swapinteger];
for(int letters = 0; letters < (int)allletters.length(); letters++)
{
if(tempstorecharacter == allletters[letters]){continue;}
word[swapinteger] = allletters[letters];
int tempvalue = value;
tempvalue -= LetterValues[tempstorecharacter] - LetterValues[allletters[letters]];
if(k20.find(word) != k20.end())
{
for(int soli = 0; soli < 3; soli++) //It does have length 10 but we only want top 3.
{
if(solutions[soli].second < tempvalue)
{
solutions[soli] = make_pair(word,tempvalue);
for(int w = 0; w < 11; w++)
{
solutionPaths[soli][w] = make_pair(pathtaken[w].first,pathtaken[w].second);
}
break;
}
}
}
}
word[swapinteger] = tempstorecharacter;
}
}
//NOW We do the recusrion depth searches
if((i+1 != 5) && j+1 != 5 && havebeen.find(make_pair(i+1,j+1)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i+1, j+1, newDepth,swap);}
if((i+1 != 5) && havebeen.find(make_pair(i+1,j)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i+1, j, newDepth,swap);}
if((i+1 != 5) && j-1 != -1 && havebeen.find(make_pair(i+1,j-1)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i+1, j-1, newDepth,swap);}
if( j+1 != 5 && havebeen.find(make_pair(i,j+1)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i, j+1, newDepth,swap);}
if( j-1 != -1 && havebeen.find(make_pair(i,j-1)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i, j-1, newDepth,swap);}
if((i-1 != -1) && j+1 != 5 && havebeen.find(make_pair(i-1,j+1)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i-1, j+1, newDepth,swap);}
if((i-1 != -1) && havebeen.find(make_pair(i-1,j)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i-1, j, newDepth,swap);}
if((i-1 != -1) && j-1 != -1 && havebeen.find(make_pair(i-1,j-1)) == havebeen.end()){recsolve(values, k20, pathtaken, solutions, doublepoints, i-1, j-1, newDepth,swap);}
pathtaken[depth-1] = make_pair(-1,-1);
//clear our path.
return;
}
int main()
{
LetterValues['a'] = 1;
LetterValues['e'] = 1;
LetterValues['i'] = 1;
LetterValues['o'] = 1;
LetterValues['n'] = 2;
LetterValues['r'] = 2;
LetterValues['s'] = 2;
LetterValues['t'] = 2;
LetterValues['d'] = 3;
LetterValues['g'] = 3;
LetterValues['l'] = 3;
LetterValues['b'] = 4;
LetterValues['h'] = 4;
LetterValues['p'] = 4;
LetterValues['m'] = 4;
LetterValues['u'] = 4;
LetterValues['y'] = 4;
LetterValues['c'] = 5;
LetterValues['f'] = 5;
LetterValues['v'] = 5;
LetterValues['w'] = 5;
LetterValues['k'] = 6;
LetterValues['j'] = 7;
LetterValues['x'] = 7;
LetterValues['q'] = 8;
LetterValues['z'] = 8;
//take in the 20k words and put that into a dictonary
unordered_map<string,bool> k20;
pair<char,int> values[5][5];
//get board state from user and get in 20k words
string line;
ifstream rfile;
rfile.open("20k.txt");
if (rfile.is_open())
{
while (getline(rfile, line))
{
k20[line] = true;
}
rfile.close();
}
//
rfile.open("board.txt");
int swap = 0;
pair<int,int> doublepoints = make_pair(-1,-1);
if (rfile.is_open())
{
int tempi = 0;
char tempc = ' ';
bool doubleletterpoints = false;
bool tripleletterpoints = false;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
rfile >> tempc;
if(tempc == '#'){
doublepoints = make_pair(i,j);
rfile >> tempc;
}
if(tempc == '/'){
rfile >> tempc;
doubleletterpoints = true;
}
if(tempc == '?')
{
rfile >> tempc;
tripleletterpoints = true;
}
tempi = LetterValues[tempc] + (LetterValues[tempc]*doubleletterpoints) + (LetterValues[tempc]*2*tripleletterpoints);
doubleletterpoints = false;
tripleletterpoints = false;
values[i][j] = make_pair(tempc, tempi);
cout << values[i][j].first << " ";
cout << values[i][j].second << " ";
}
cout << "\n";
}
rfile >> swap;
rfile.close();
}
//NEW ELEMENT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pair<string,int> solutions[3];
for(int i = 0; i < 3; i++)
{
solutions[i] = make_pair("NULL",-1);
}
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
pair<int,int> pathtaken[11];
for(int k = 1; k < 11; k++){pathtaken[k] = make_pair(-1,-1);}
pathtaken[0] = make_pair(i,j);
recsolve(values,k20,pathtaken,solutions,doublepoints,i,j,1,swap);
}
}
cout << solutions[0].first << endl;
cout << solutions[0].second << endl;
cout << endl;
cout << "0,0 on the top left. right inc x down inc y" << endl;
cout << "X" << " Y" << endl;
for(int i = 0; i < 8; i++)
{
int j = solutionPaths[0][i].second;
int w = solutionPaths[0][i].first;
cout << solutionPaths[0][i].second << " " << solutionPaths[0][i].first << " " << values[w][j].first << endl;
}
return 0;
}