1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 4 using namespace std; 5 const int maxn = 25; 6 const int ra[maxn][maxn] = {{0,2,6,11,15,20,22},{1,3,8,12,17,21,23}, 7 {10,9,8,7,6,5,4},{19,18,17,16,15,14,13}, 8 {23,21,17,12,8,3,1},{22,20,15,11,6,2,0}, 9 {13,14,15,16,17,18,19},{4,5,6,7,8,9,10}}; 10 const int ja[maxn] = {6,7,8,11,12,15,16,17}; 11 int a[maxn]; 12 string rnt; 13 14 int readint() {int tmp;scanf("%d",&tmp);return tmp;} 15 16 int unordered(int tmpa[maxn]) 17 { 18 int cnt = 0; 19 _for(i,0,8) 20 if(tmpa[ja[i]]!=1) 21 cnt ++; 22 return cnt; 23 } 24 25 void init(int ta,int tmpa[maxn]) 26 { 27 memcpy(tmpa,a,sizeof(a)); 28 _for(i,0,24) 29 if(tmpa[i]!=ta) 30 tmpa[i] = 0; 31 else 32 tmpa[i] = 1; 33 } 34 35 int g2i(int tmpa[maxn]) 36 { 37 int rnt = 0; 38 for(int i = 23;i >= 0;i --) 39 if(tmpa[i]) 40 rnt += 1<<(23-i); 41 return rnt; 42 } 43 44 void i2g(int I,int tmpa[maxn]) 45 { 46 memset(tmpa,0,sizeof(int)*maxn); 47 for(int i = 23;i >= 0;i --) 48 if((I>>i)&0x1) tmpa[23-i] = 1; 49 } 50 51 void rotate(int di,int tmpa[maxn]) 52 { 53 int t = tmpa[ra[di][0]]; 54 _for(i,1,7) 55 tmpa[ra[di][i-1]] = tmpa[ra[di][i]]; 56 tmpa[ra[di][6]] = t; 57 } 58 59 struct cmp 60 { 61 bool operator() (const pair<int,string> a,const pair<int,string> b) const 62 { 63 int tmpa[maxn]; 64 int tmpb[maxn]; 65 i2g(a.first,tmpa);i2g(b.first,tmpb); 66 int aa = unordered(tmpa),bb = unordered(tmpb); 67 if(aa>bb) 68 return true; 69 return false; 70 } 71 }; 72 73 int flag = 0; 74 string bfs(int ta) 75 { 76 int tmpa[maxn]; 77 set<int> vis; 78 string bs; 79 init(ta,tmpa); 80 if(!unordered(tmpa)) {flag = 1;return bs;} 81 82 queue<pair<int,string>> q; 83 q.push({g2i(tmpa),bs}); 84 while(!q.empty()) 85 { 86 int g = q.front().first; 87 string s = q.front().second; 88 i2g(g,tmpa); 89 if(!unordered(tmpa)) return s; 90 q.pop(); 91 _for(i,0,8) 92 { 93 i2g(g,tmpa); 94 rotate(i,tmpa); 95 96 int ttg = g2i(tmpa); 97 if(!vis.count(ttg)) 98 { 99 vis.insert(ttg); 100 s += i+'A'; 101 q.push({ttg,s}); 102 s.pop_back(); 103 } 104 } 105 } 106 return bs; 107 } 108 109 int solve() 110 { 111 string rnts; 112 rnt.clear(); 113 rnt = bfs(1); 114 int nnuumm = 1; 115 _for(i,2,4) 116 { 117 rnts = bfs(i); 118 if(rnts.size()<rnt.size()||rnts.size()==rnt.size()&&rnts<rnt) 119 { 120 rnt = rnts; 121 nnuumm = i; 122 } 123 } 124 return nnuumm; 125 } 126 127 void print_ans(int ans) 128 { 129 if(flag) 130 { 131 cout<<"No moves needed"<<endl; 132 cout << a[6] << endl; 133 return ; 134 } 135 cout << rnt << endl; 136 cout << ans << endl; 137 } 138 139 int main() 140 { 141 while(~scanf("%d",&a[0])&&a[0]!=0) 142 { 143 flag = 0; 144 _for(i,1,24) 145 a[i] = readint(); 146 int ans = solve(); 147 print_ans(ans); 148 } 149 return 0; 150 }
这是一份又臭又长,没有用IDA*所以慢的一塌糊涂就算时间限制再加个0都过不了的BFS无优化代码,放上来纯属个人娱乐,主要具有纪念意义,没有任何参考价值