-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.cpp
More file actions
252 lines (234 loc) · 6.07 KB
/
cube.cpp
File metadata and controls
252 lines (234 loc) · 6.07 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <list>
#include <algorithm>
#include <tuple>
#include "cube.h"
std::unordered_set<std::string> words;
struct Cubie cubies[64];
std::unordered_map<char, std::vector<int>> letterMap;
int wordCount = 0;
int copyNeighbs(int sheet, int cube) {
int i;
for (i = 0; i < 8 && cub[cube%16][i] >= 0; i++) {
cubies[cube].neighb.push_back(cub[cube%16][i] + (sheet*16));
}
return i;
}
void SetNeighbors() {
//sorry this feels super hacky but my brain won't brain better
for (int i = 0; i < 4; i++){
for (int j = 0; j < 16; j++) {
copyNeighbs(i,16*i+j);
switch(i) {
case 0:
cubies[j].neighb.push_back(16+j);
copyNeighbs(1,j);
break;
case 1:
case 2:
cubies[16*i+j].neighb.push_back(16*(i+1)+j);
cubies[16*i+j].neighb.push_back(16*(i-1)+j);
copyNeighbs(i+1, 16*i+j);
copyNeighbs(i-1, 16*i+j);
break;
case 3:
cubies[48+j].neighb.push_back(32+j);
copyNeighbs(2,48+j);
break;
default:
std::cout << "We've got problems...";
}
}
}
/*
for (int i = 0; i < 64; i++) {
std::cout << i << ": ";
for (int j = 0; j<cubies[i].neighb.size(); j++) {
std::cout << cubies[i].neighb[j] << " ";
}
std::cout << std::endl;
}*/
}
int ReadCube(FILE *f_c) {
char letters[65];
if (fscanf(f_c, "%s", letters) == EOF) {
//done!
return 1;
}
for (int i = 0; i < 64; i++) {
cubies[i].letter = letters[i];
letterMap[letters[i]].push_back(i);
}
return 0;
}
void BFS(std::string word) {
std::list<std::vector<int>> paths;
std::vector<int> tempVec;
try {
tempVec = (letterMap.at(word[0]));
}
catch(const std::out_of_range &e) {
//first letter of word does not exist in cube
return;
}
std::cout << "tempVec: ";
for (int i = 0; i < tempVec.size(); i++)
std::cout << tempVec[i] << " ";
std::cout << std::endl;
std::vector<int>::iterator itr;
for (itr = tempVec.begin(); itr != tempVec.end(); itr++) {
//push all first letters into paths
std::vector<int> first(*itr);
paths.push_front(first);
}
std::cout << word << std::endl;
for (int w = 1; w < word.size(); w++) {
std::list<std::vector<int>>::iterator p;
for (auto p = paths.begin(); p != paths.end(); ) {
//number of paths could change with every iteration of w
//std::cout << cubies[(*p).back()].neighb.size() << std::endl;
int neighbSize = cubies[(*p).back()].neighb.size();
for (int n = 0; n < neighbSize; n++) {
for (int i = 0; i < (*p).size(); i++)
std::cout << (*p)[i] << " ";
std::cout << std::endl;
//check each neighbor of the last node of the path
int ni = cubies[(*p).back()].neighb[n];
if (cubies[ni].letter = word[w]) {
//check if this cubbie has already been used in this path
std::vector<int>::iterator it = std::find((*p).begin(), (*p).end(), ni);
if (it == (*p).end()) {
std::vector<int> newPath(*p);
//add new cubies to back of path
newPath.push_back(ni);
//add new paths to the front of list
paths.push_front(newPath);
}
}
}
//old path is too short now and has been replaced (maybe) by new paths: delete p
//erase returns iterator to next element: this is how we advance p
p = paths.erase(p);
}
std::cout << "finished pi " << std::endl;
}
std::cout << "ending" << std::endl;
if (!paths.empty()) {
std::cout << word << " found\n";
wordCount++;
}
}
void DFS2(std::string word) {
std::stack<std::tuple<int, int>> paths;
int used[word.size()];
std::vector<int> tempVec;
try {
tempVec = (letterMap.at(word[0]));
}
catch(const std::out_of_range &e) {
//first letter of word does not exist in cube
return;
}
if (word.size() == 1) {
//word is only one letter, and that letter exists in cube
wordCount++;
return;
}
std::vector<int>::iterator itr;
for (itr = tempVec.begin(); itr != tempVec.end(); itr++) {
//push all first letters into paths
std::tuple<int, int> first(*itr, 0);
paths.push(first);
}
while (!paths.empty()) {
std::tuple<int,int> node = paths.top();
int loc = std::get<0>(node);
int let = std::get<1>(node);
paths.pop();
used[let] = loc;
//check all neighbors of this (correct) letter:
//if any neighbor is a valid option(correct letter
//and not yet used), push to paths
for (int i = 0; i < cubies[loc].neighb.size(); i++) {
int curN = cubies[loc].neighb[i];
if (cubies[curN].letter == word[let+1]) {
bool alreadyUsed = false;
for (int i = 0; i < let+1; i++) {
if (used[i] == curN) {
alreadyUsed = true;
break;
}
}
if (!alreadyUsed) {
if (let+1 == word.size()-1){
//at the end of the word- found a path!
wordCount++;
return;
}
std::tuple<int,int> newTup = std::make_tuple(curN, let+1);
paths.push(newTup);
}
}
}
}
}
int main(int argc, char *argv[]) {
std::vector<int> output;
output.reserve(1000);
//set the list of neighbors for each cubbie
SetNeighbors();
// read in word-file
FILE *f_w = fopen(argv[2], "r");
if (f_w == NULL) {
std::cout << "unable to open word-file\n";
return -1;
}
char tempWord[50];
while(fscanf(f_w, "%s", tempWord) != EOF) {
bool punc = false;
for (int i=0; i < 50 && tempWord[i] != '\0'; i++) {
if (isalpha(tempWord[i])) {
char c = tempWord[i];
tempWord[i] = tolower(c);
} else {
//word contains punctuation/numeric character: do not add
punc = true;
break;
}
}
if (!punc) {
//add word to set
words.insert(tempWord);
}
}
fclose(f_w);
//read in cube-file (as appropriate)
FILE *f_c = fopen(argv[1], "r");
if (f_c == NULL) {
std::cout << "unable to open cube-file\n";
return -1;
}
while (ReadCube(f_c) == 0){
wordCount = 0;
std::unordered_set<std::string>::iterator itr;
for(itr = words.begin(); itr != words.end(); itr++) {
for (int i = 0; i < 64; i++) {
cubies[i].used = false;
}
DFS2(*itr);
}
letterMap.clear();
output.push_back(wordCount);
//std::cout << wordCount << std::endl;
}
for (int i = 0; i < output.size(); i++) {
std::cout << output[i] << std::endl;
}
fclose(f_c);
return 0;
}