-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSolver.cpp
203 lines (189 loc) · 3.91 KB
/
Solver.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
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
#include "Solver.hpp"
#include <fstream>
#include <thread>
map<int64_t, string> phaseHash[4];
char moves[6] = {'F','R','U','B','L','D'};
int phase = 1;
void readData(std::string file, int phase)
{
ifstream input(file);
if (input.fail())
{
cout << RED << "File " << file << " not found >.<" << endl;
exit(1);
}
int64_t hash;
string moves;
while (input >> hash >> moves)
phaseHash[phase - 1][hash] = moves;
}
Solver::Solver(Cube c) {
Cube tmp;
for (int i = 0; i < 18; i++)
allowedMoves[i] = 1;
for (int i = 1 ; i <= 4; i++)
phaseGoal[i] = getPhaseId(tmp, i);
thread th1(readData, "./database/phase1", 1);
thread th2(readData, "./database/phase2", 2);
thread th3(readData, "./database/phase3", 3);
thread th4(readData, "./database/phase4", 4);
th1.join();
th2.join();
th3.join();
th4.join();
}
void Solver::nextPhase(){
switch (phase){
case 1:
allowedMoves[0] = 0;
allowedMoves[2] = 0;
allowedMoves[9] = 0;
allowedMoves[11] = 0;
break;
case 2:
allowedMoves[3] = 0;
allowedMoves[5] = 0;
allowedMoves[12] = 0;
allowedMoves[14] = 0;
break;
case 3:
allowedMoves[6] = 0;
allowedMoves[8] = 0;
allowedMoves[15] = 0;
allowedMoves[17] = 0;
}
phase++;
}
int64_t Solver::idPhase1(Cube c){
int64_t id = 0;
for (int i = 0; i < 12; i++)
{
id <<= 1;
id += c.eOri[i];
}
return id;
}
int64_t Solver::idPhase2(Cube c){
int64_t id = 0;
for (int i = 0; i < 8; i++)
{
id <<= 2;
id += c.cOri[i];
}
for (int i = 0; i < 12; i++){
id <<= 2;
if (c.ePos[i] < 8)
id++;
}
return id;
}
int64_t Solver::idPhase3(Cube c){
string faces = "FRUBLD";
int64_t id = 0;
for (int i = 0; i < 7; i++){
for (int j = 0; j < 3; j++){
id <<= 1;
char t = c.cornerNames[c.cPos[i]][(c.cOri[i] + j) % 3];
if (!(t == c.cornerNames[i][j] ||
t == faces[(faces.find(c.cornerNames[i][j]) + 3) % 6]))
id++;
}
}
for (int i = 0; i < 11; i++){
for (int j = 0; j < 2; j++){
id <<= 1;
char t = c.edgeNames[c.ePos[i]][(c.eOri[i] + j) % 2];
if (!(t == c.edgeNames[i][j] ||
t == faces[(faces.find(c.edgeNames[i][j]) + 3) % 6]))
id++;
}
}
for (int i = 0; i < 8; i++)
{
id <<= 1;
if (c.cPos[i] % 4 != i % 4)
id++;
}
id <<= 1;
for (int i = 0; i < 8; i++ )
for( int j = i + 1; j < 8; j++ )
id ^= c.cPos[i] > c.cPos[j];
return id;
}
int64_t Solver::idPhase4(Cube c){
string faces = "FRUBLD";
int64_t id = 0;
for (int i = 0; i < 8; i++){
for (int j = 0; j < 3; j++){
id <<= 1;
char t = c.cornerNames[c.cPos[i]][(c.cOri[i] + j) % 3];
if (t == faces[(faces.find(c.cornerNames[i][j]) + 3) % 6])
id++;
}
}
for (int i = 0; i < 12; i++){
for (int j = 0; j < 2; j++){
id <<= 1;
char t = c.edgeNames[c.ePos[i]][(c.eOri[i] + j) % 2];
if (t == faces[(faces.find(c.edgeNames[i][j]) + 3) % 6])
id++;
}
}
return id;
}
int64_t Solver::getPhaseId(Cube c, int phase)
{
int64_t id = 0;
id = (*this.*(idPhase[phase - 1]))(c);
return id;
}
Cube Solver::BFS(int step, queue<Cube> level)
{
if (step == 0){
ids.clear();
int64_t num = getPhaseId(level.front(), phase);
if (num == phaseGoal[phase]){
nextPhase();
return level.front();
}
}
queue<Cube> next;
Cube cur;
while (!level.empty()){
cur = level.front();
level.pop();
int count = 0;
for (int move = 0; move < 6; move++)
{
for (int amount = 0; amount < 3; amount++)
{
cur.rotCube(moves[move], 1);
int64_t id;
if (allowedMoves[count] == 1){
id = getPhaseId(cur, phase);
if (ids.find(id) == ids.end())
{
cur.path += moves[move];
cur.path += (amount + '1');
if (id == phaseGoal[phase])
{
nextPhase();
return cur;
}
ids.insert(id);
next.push(cur);
cur.path = cur.path.substr(0, cur.path.length() - 2);
}
}
count++;
}
cur.rotCube(moves[move], 1);
}
}
if (next.empty())
{
cout << RED << "Solution not found\n";
exit(0);
}
return BFS(step + 1, next);
}