Repository navigation
Expand file tree
/
Copy pathMagic3.cpp
More file actions
528 lines (525 loc) · 12.8 KB
/
Copy pathMagic3.cpp
File metadata and controls
528 lines (525 loc) · 12.8 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
struct Node{
string state;
string rath;
Node(){}
Node(string s,string r){
state = s; rath = r;
}
}node;
struct Path{
string rath;
int step;
Path(){}
Path(string r,int s)
{
rath = r;
step = s;
}
bool operator < (const Path& rhs)const
{
return ((step<rhs.step)||(step==rhs.step&&rath<rhs.rath));
}
void show(int id)
{
cout<<setw(2)<<id;
cout<<". "<<rath<<" ("<<step<<"步)\n";
}
};
typedef unordered_map<string,string> uss;
typedef queue<Node> qN;
uss::iterator it;
string op[9] = {
"R2","U2","F2","R","U","F","R\'","U\'","F\'"
};
map<string,int> getOPNum;
int tR[3][5] = {{2,18,6,20,2},{1,17,5,23,1},{15,14,13,12,15}};
int eR[2][5] = {{1,17,5,23,1},{12,15,14,13,12}};
int tR2[3][4] = {{1,2,5,6},{17,18,23,20},{12,13,14,15}};
int eR2[2][4] = {{1,17,5,23},{12,13,14,15}};
int tU[3][5] = {{3,2,1,0,3},{12,20,8,16,12},{13,21,9,17,13}};
int eU[2][5] = {{3,2,1,0,3},{8,16,12,20,8}};
int tU2[3][4] = {{0,1,2,3},{8,9,12,13},{16,17,20,21}};
int eU2[2][4] = {{0,1,2,3},{8,16,12,20}};
int tF[3][5] = {{19,18,17,16,19},{2,9,4,15,2},{3,10,5,12,3}};
int eF[2][5] = {{19,18,17,16,19},{2,9,4,15,2}};
int tF2[3][4] = {{16,17,18,19},{2,3,4,5},{9,10,15,12}};
int eF2[2][4] = {{2,15,4,9},{16,17,18,19}};
int tx[6][4] = {{0,1,2,3},{16,17,18,19},{4,5,6,7},{22,23,20,21},{15,14,13,12},{8,9,10,11}};
int tx2[4][8] = {{0,1,2,3,4,5,6,7},{16,17,18,19,22,23,20,21},{8,9,10,11},{15,14,13,12}};
int ty[6][4] = {{8,9,10,11},{16,17,18,19},{12,13,14,15},{20,21,22,23},{0,1,2,3},{4,5,6,7}};
int ty2[4][8] = {{8,9,10,11,12,13,14,15},{16,17,18,19,20,21,22,23},{3,2,1,0},{4,5,6,7}};
int tz[6][4] = {{0,1,2,3},{11,8,9,10},{6,7,4,5},{13,14,15,12},{19,18,17,16},{20,21,22,23}};
int tz2[4][8] = {{0,1,2,3,6,7,4,5},{8,9,10,11,14,15,12,13},{0,1,2,3},{4,5,6,7}};
void cR (string,string&);
void cR_(string,string&);
void cR2(string,string&);
void cU (string,string&);
void cU_(string,string&);
void cU2(string,string&);
void cF (string,string&);
void cF_(string,string&);
void cF2(string,string&);
void cx (string,string&);
void cx_(string,string&);
void cx2(string,string&);
void cy (string,string&);
void cy_(string,string&);
void cy2(string,string&);
void cz (string,string&);
void cz_(string,string&);
void cz2(string,string&);
char opColor(char);
string rev_op(string);
void f(string,string&,int);
string get_rev_rath(string);
int get_step(string);
int simplify_rath(string&);
string getState(const string&);
void two_dir_bfs(string st,string ed,int Daluan_step)
{
set<string> Set;
// int MinStep = 200;
// vector<string> v;
if(st==ed)
{
puts("The magic cube is already perfect.");
return;
}
uss mp,rmp;
queue<string> Q,rQ;
string rath(""),nexrath,now,to,ans,res;
int sz = 0, rsz = 0, nowStep = 0,Len;
mp[st] = rath;
rmp[ed] = rath;
Q.push(st);
rQ.push(ed);
while(!Q.empty()||!rQ.empty())
{
sz = Q.size(), rsz = rQ.size();
if(sz<=rsz&&!Q.empty())
{
now = Q.front();Q.pop();
rath = mp[now];
for(int i=3;i<9;++i)
{
f(now,to,i);
it = mp.find(to);
if(it!=mp.end()) continue;
nexrath = rath+op[i];
it = rmp.find(to);
if(it!=rmp.end())
{
ans = nexrath + get_rev_rath(it->second);
int _step = simplify_rath(ans);
if(_step<=Daluan_step+5)
{
// cout<<"还原:"<<ans<<"\n";
// cout<<"步数:"<<_step<<endl;
Set.insert(ans);
if(Set.size()>=99)
{
vector<Path> vp;
for(auto x:Set)
vp.push_back(Path(x,get_step(x)));
sort(vp.begin(),vp.end());
Len = vp.size();
for(int i=0;i<Len;++i)
vp[i].show(i+1);
return;
}
}
// return;
}
Q.push(to);
mp[to] = nexrath;
// cout<<nexrath<<": "<<to<<endl;
}
}
else
{
now = rQ.front();rQ.pop();
rath = rmp[now];
for(int i=3;i<9;++i)
{
f(now,to,i);
it = rmp.find(to);
if(it!=rmp.end()) continue;
nexrath = rath + op[i];
it = mp.find(to);
if(it!=mp.end())
{
ans = (it->second) + get_rev_rath(nexrath);
int _step = simplify_rath(ans);
if(_step<=Daluan_step+5)
{
// cout<<"还原:"<<ans<<"\n";
// cout<<"步数:"<<_step<<endl;
Set.insert(ans);
if(Set.size()>=99)
{
vector<Path> vp;
for(auto x:Set)
vp.push_back(Path(x,get_step(x)));
sort(vp.begin(),vp.end());
Len = vp.size();
for(int i=0;i<Len;++i)
vp[i].show(i+1);
return;
}
}
// return;
}
rQ.push(to);
rmp[to] = rath+op[i];
// cout<<nexrath<<": "<<to<<endl;
}
}
}
vector<Path> vp;
for(auto x:Set)
vp.push_back(Path(x,get_step(x)));
sort(vp.begin(),vp.end());
Len = vp.size();
for(int i=0;i<Len;++i)
vp[i].show(i+1);
}
//char st[50],Up[10],Down[10],Left[10],Right[10],Front[10],Back[10];
typedef char str50[100];
str50 st,Up,Down,Left,Right,Front,Back,Daluan;
str50 _u,_d,_l,_r,_f,_b;
string get_ed(str50 Up,str50 Down,str50 Left,str50 Right,str50 Front,str50 Back)
{
string ed;
char dColor = Down[3],bColor = Back[2],lColor = Left[3];
char uColor = opColor(dColor);
char fColor = opColor(bColor);
char rColor = opColor(lColor);
string tmp = string("")+uColor+dColor+lColor+rColor+fColor+bColor;
int cnt = 0;
for(int i=0;i<6;++i)
for(int j=0;j<4;++j)
ed += tmp[i];
return ed;
}
int main()
{
for(int i=0;i<9;++i)
getOPNum[op[i]] = i;
// string sdf;cin>>sdf;cout<<getState(sdf)<<endl;return 0;
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// freopen("out.txt","w",stdout);
// string Up,Down,Left,Right,Front,Back;R2F'R2U2FU'F2UR2 BYGGYGWWORRBYRBWWOOBGYOR
while(scanf("%s%s",Daluan,st)!=EOF)
{
int Daluan_step = get_step(Daluan);
sscanf(st,"%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s",Up,Down,Left,Right,Front,Back,_u,_d,_l,_r,_f,_b);
cout<<"黄顶红前打乱: "<<Daluan<<endl;
cout<<"初始状态为:"<<st<<endl;
string corner_ed = get_ed(Up,Down,Left,Right,Front,Back);
string edge_ed = get_ed(_u,_d,_l,_r,_f,_b);
string ed = corner_ed+edge_ed;
cout<<"最终状态为:"<<ed<<endl;
two_dir_bfs(st,ed,Daluan_step);
// bfs(st,ed);
// BFS(st,ed);
}
fclose(stdin);
// fclose(stdout);
return 0;
}
char opColor(char c)
{
switch (c){
case 'R': return 'O';
case 'O': return 'R';
case 'B': return 'G';
case 'G': return 'B';
case 'Y': return 'W';
case 'W': return 'Y';
}
return '0';
}
string rev_op(string a)
{
if(a=="R") return string("R\'");
else if(a=="R\'") return string("R");
else if(a=="U") return string("U\'");
else if(a=="U\'") return string("U");
else if(a=="F") return string("F\'");
else if(a=="F\'") return string("F");
else return a;
}
void f(string a,string& b,int op)
{
switch (op){
case 0:cR2(a,b);break;
case 1:cU2(a,b);break;
case 2:cF2(a,b);break;
case 3: cR(a,b);break;
case 4: cU(a,b);break;
case 5: cF(a,b);break;
case 6:cR_(a,b);break;
case 7:cU_(a,b);break;
case 8:cF_(a,b);break;
}
}
void cR(string a,string& b)
{
b = a;
for(int i=0;i<3;++i) //corner block
for(int j=0;j<4;++j)
b[tR[i][j]] = a[tR[i][j+1]];
for(int i=0;i<2;++i) //edge block
for(int j=0;j<4;++j)
b[eR[i][j]+24] = a[eR[i][j+1]+24];
}
void cR_(string a,string& b)
{
b = a;
for(int i=0;i<3;++i) //corner block
for(int j=4;j>0;--j)
b[tR[i][j]] = a[tR[i][j-1]];
for(int i=0;i<2;++i) //edge block
for(int j=4;j>0;--j)
b[eR[i][j]+24] = a[eR[i][j-1]+24];
}
void cR2(string a,string& b)
{
b = a;
for(int i=0;i<3;++i)
for(int j=0;j<4;++j)
b[tR2[i][j]] = a[tR2[i][(j+2)%4]];
for(int i=0;i<2;++i)
for(int j=0;j<4;++j)
b[eR2[i][j]+24] = a[eR2[i][(j+2)%4]+24];
}
void cU(string a,string &b)
{
b = a;
for(int i=0;i<3;++i) //corner block
for(int j=0;j<4;++j)
b[tU[i][j]] = a[tU[i][j+1]];
for(int i=0;i<2;++i) //edge block
for(int j=0;j<4;++j)
b[eU[i][j]+24] = a[eU[i][j+1]+24];
}
void cU_(string a,string &b)
{
b = a;
for(int i=0;i<3;++i)//corner block
for(int j=4;j>0;--j)
b[tU[i][j]] = a[tU[i][j-1]];
for(int i=0;i<2;++i) //edge block
for(int j=4;j>0;--j)
b[eU[i][j]+24] = a[eU[i][j-1]+24];
}
void cU2(string a,string &b)
{
b = a;
for(int i=0;i<3;++i)
for(int j=0;j<4;++j)
b[tU2[i][j]] = a[tU2[i][(j+2)%4]];
for(int i=0;i<2;++i)
for(int j=0;j<4;++j)
b[eU2[i][j]+24] = a[eU2[i][(j+2)%4]+24];
}
void cF(string a,string& b)
{
b = a;
for(int i=0;i<3;++i) //corner block
for(int j=0;j<4;++j)
b[tF[i][j]] = a[tF[i][j+1]];
for(int i=0;i<2;++i) //edge block
for(int j=0;j<4;++j)
b[eF[i][j]+24] = a[eF[i][j+1]+24];
}
void cF_(string a,string& b)
{
b = a;
for(int i=0;i<3;++i) //corner block
for(int j=4;j>0;--j)
b[tF[i][j]] = a[tF[i][j-1]];
for(int i=0;i<2;++i) //edge block
for(int j=4;j>0;--j)
b[eF[i][j]+24] = a[eF[i][j-1]+24];
}
void cF2(string a,string& b)
{
b = a;
for(int i=0;i<3;++i)
for(int j=0;j<4;++j)
b[tF2[i][j]] = a[tF2[i][(j+2)%4]];
for(int i=0;i<2;++i)
for(int j=0;j<4;++j)
b[eF2[i][j]+24] = a[eF2[i][(j+2)%4]+24];
}
string get_rev_rath(string a)
{
string b = string("");
int len = a.length();
int i = 0;
stack<string> sta;
while(i<len)
{
if(a[i]!='R'&&a[i]!='U'&&a[i]!='F') return false;
string now = "";
now +=a[i];
if(i+1<len&&(a[i+1]=='2'||a[i+1]=='\'')) now+=a[i+1],i+=2;
else ++i;
sta.push(rev_op(now));
}
while(!sta.empty())
{
b += sta.top();
sta.pop();
}
return b;
}
int get_step(string a)
{
int len = a.length();
int i = 0, step = 0;
while(i<len)
{
if(i+1<len&&(a[i+1]=='2'||a[i+1]=='\'')) i+=2;
else ++i;
++step;
}
return step;
}
int simplify_rath(string &a)
{
vector<string> v;
int len = a.length();
int i = 0, step = 0;
while(i<len)
{
string tmp;
tmp += a[i];
if(i+1<len&&(a[i+1]=='2'||a[i+1]=='\'')) tmp+=a[i+1],i+=2;
else ++i;
if(step==0)
{
v.push_back(tmp); ++step;
}
else if(v[step-1]==rev_op(tmp))
{
v.pop_back();--step;
}
else if(v[step-1]==tmp)
{
if(tmp=="R"||tmp=="R\'")
v.pop_back(),v.push_back("R2");
else if(tmp=="U"||tmp=="U\'")
v.pop_back(),v.push_back("U2");
else if(tmp=="F"||tmp=="F\'")
v.pop_back(),v.push_back("F2");
else
v.push_back(tmp),++step;
}
else if((tmp=="U2"&&v[step-1]=="U")||(tmp=="U"&&v[step-1]=="U2"))
{
v.pop_back(); v.push_back("U\'");
}
else if((tmp=="U2"&&v[step-1]=="U\'")||(tmp=="U\'"&&v[step-1]=="U2"))
{
v.pop_back(); v.push_back("U");
}
else if((tmp=="R2"&&v[step-1]=="R")||(tmp=="R"&&v[step-1]=="R2"))
{
v.pop_back(); v.push_back("R\'");
}
else if((tmp=="R2"&&v[step-1]=="R\'")||(tmp=="R\'"&&v[step-1]=="R2"))
{
v.pop_back(); v.push_back("R");
}
else if((tmp=="F2"&&v[step-1]=="F")||(tmp=="F"&&v[step-1]=="F2"))
{
v.pop_back(); v.push_back("F\'");
}
else if((tmp=="F2"&&v[step-1]=="F\'")||(tmp=="F\'"&&v[step-1]=="F2"))
{
v.pop_back(); v.push_back("F");
}
else
{
v.push_back(tmp); ++step;
}
}
a.clear();
for(int i=0;i<step;++i)
a += v[i];
return step;
}
string getState(const string& a)
{
vector<string> v;
int len = a.length();
int i = 0, step = 0;
while(i<len)
{
string tmp;
tmp += a[i];
if(i+1<len&&(a[i+1]=='2'||a[i+1]=='\'')) tmp+=a[i+1],i+=2;
else ++i;
if(step==0)
{
v.push_back(tmp);
}
else if(v[step-1]==rev_op(tmp))
{
v.pop_back();--step;
}
else if(v[step-1]==tmp)
{
if(tmp=="R"||tmp=="R\'")
v.pop_back(),v.push_back("R2");
else if(tmp=="U"||tmp=="U\'")
v.pop_back(),v.push_back("U2");
else if(tmp=="F"||tmp=="F\'")
v.pop_back(),v.push_back("F2");
else
v.push_back(tmp),++step;
}
else if((tmp=="U2"&&v[step-1]=="U")||(tmp=="U"&&v[step-1]=="U2"))
{
v.pop_back(); v.push_back("U\'");
}
else if((tmp=="U2"&&v[step-1]=="U\'")||(tmp=="U\'"&&v[step-1]=="U2"))
{
v.pop_back(); v.push_back("U");
}
else if((tmp=="R2"&&v[step-1]=="R")||(tmp=="R"&&v[step-1]=="R2"))
{
v.pop_back(); v.push_back("R\'");
}
else if((tmp=="R2"&&v[step-1]=="R\'")||(tmp=="R\'"&&v[step-1]=="R2"))
{
v.pop_back(); v.push_back("R");
}
else if((tmp=="F2"&&v[step-1]=="F")||(tmp=="F"&&v[step-1]=="F2"))
{
v.pop_back(); v.push_back("F\'");
}
else if((tmp=="F2"&&v[step-1]=="F\'")||(tmp=="F\'"&&v[step-1]=="F2"))
{
v.pop_back(); v.push_back("F");
}
else
{
v.push_back(tmp); ++step;
}
}
string st = "YYYYWWWWBBBBGGGGRRRROOOOYYYYWWWWBBBBGGGGRRRROOOO";
string ed;
for(int i=0;i<step;++i)
{
f(st,ed,getOPNum[v[i]]);
st = ed;
}
return ed;
}