解法もプログラムしてみました。
こちらの攻略法の一部を利用しています。
2×2のルービックキューブ攻略
攻略法の場合分けは全ては実装せず、一部のパターン外の時、再度作り直ししています。
<Cubeクラス>
rangemove 部分回転させる
pmatch (x,y,表面の記号)のリストで判定
seq キューブを動かす解法の手順を実行
storeとback
手順を実行後、パターン外のやり直し用
stsはフロー制御用の変数
作成後なんどか測ってみて最速で0.007秒
こちらの攻略法の一部を利用しています。
2×2のルービックキューブ攻略
攻略法の場合分けは全ては実装せず、一部のパターン外の時、再度作り直ししています。
<Cubeクラス>
rangemove 部分回転させる
pmatch (x,y,表面の記号)のリストで判定
seq キューブを動かす解法の手順を実行
storeとback
手順を実行後、パターン外のやり直し用
stsはフロー制御用の変数
作成後なんどか測ってみて最速で0.007秒
import copy import random import time class Cube: def __init__(self): self.bd="" self.cp="" self.bf=["" for i in range(8)] self.af=["" for i in range(8)] self.bd+=" BB " self.bd+=" BB " self.bd+=" UU " self.bd+=" UU " self.bd+="LLFFRR" self.bd+="LLFFRR" self.bd+=" DD " self.bd+=" DD " self.mv=[] self.set_mv() self.mem=[] self.wk="" self.tc=0 def store(self): self.wk=self.bd def back(self): self.bd=self.wk def pmatch(self,a): for i in a: x,y,s=i if self.bd[x+y*6]!=s: return False return True def countface(self): r=0 if self.bd[2]==self.bd[3]==self.bd[8]==self.bd[9]:r+=1 if self.bd[14]==self.bd[15]==self.bd[20]==self.bd[21]:r+=1 if self.bd[26]==self.bd[27]==self.bd[32]==self.bd[33]:r+=1 if self.bd[38]==self.bd[39]==self.bd[44]==self.bd[45]:r+=1 if r==0:return 0 if self.bd[24]==self.bd[25]==self.bd[30]==self.bd[31]:r+=1 if self.bd[28]==self.bd[29]==self.bd[34]==self.bd[35]:r+=1 return r def write(self): print(str(self.tc)+"turn") for i in range(8): print(self.bd[i*6:i*6+6]) def search(self,arr,s): for x in range(6): for y in range(8): if arr[y][x]==s:return (x,y) def movedataset(self): arr=["1","2","3","4","5","6","7","8","9","A","B","C"] t=[] for s in arr: x,y=self.search(self.bf,s) tx,ty=self.search(self.af,s) t.append((x+y*6,tx+ty*6)) self.mv.append(t) def turn(self,rt): self.cp=self.bd for p in self.mv[rt]: p0,p1=p self.cp=self.cp[:p1]+self.bd[p0]+self.cp[p1+1:] self.tc+=1 self.bd=self.cp def seqD(self): self.seq("llluuulur uuulllulrrr") def seqE(self): self.seq("lllrurrruuul uruuurrr") def seq(self,s): #"rlfbud" s=s.replace(" ","") for i in s: self.turn("rlfbud".find(i)) def move(self,p,p1): self.cp=self.cp[:p1]+self.bd[p]+self.cp[p1+1:] def randmove(self): rt=random.randint(0,5) #self.mem.append(rt) self.turn(rt) def rangemove(self,a): "rlfbud" rt=a[random.randint(0,len(a)-1)] #self.mem.append(rt) self.turn(rt) def memrev(self): l=len(self.mem) if l==0:return for i in range(3): self.turn(self.mem[l-1]) del self.mem[l-1] def set_mv(self): #r self.bf[0]=" .1 " self.bf[1]=" .2 " self.bf[2]=" .3 " self.bf[3]=" .4 " self.bf[4]="...59A" self.bf[5]="...6CB" self.bf[6]=" .7 " self.bf[7]=" .8 " self.af[0]=" .3 " self.af[1]=" .4 " self.af[2]=" .5 " self.af[3]=" .6 " self.af[4]="...7C9" self.af[5]="...8BA" self.af[6]=" .1 " self.af[7]=" .2 " self.movedataset() #l self.bf[0]=" 1. " self.bf[1]=" 2. " self.bf[2]=" 3. " self.bf[3]=" 4. " self.bf[4]="9A5..." self.bf[5]="CB6..." self.bf[6]=" 7. " self.bf[7]=" 8. " self.af[0]=" 7. " self.af[1]=" 8. " self.af[2]=" 1. " self.af[3]=" 2. " self.af[4]="C93..." self.af[5]="BA4..." self.af[6]=" 5. " self.af[7]=" 6. " self.movedataset() #f self.bf[0]=" .. " self.bf[1]=" .. " self.bf[2]=" .. " self.bf[3]=" 12 " self.bf[4]=".89A3." self.bf[5]=".7CB4." self.bf[6]=" 65 " self.bf[7]=" .. " self.af[0]=" .. " self.af[1]=" .. " self.af[2]=" .. " self.af[3]=" 78 " self.af[4]=".6C91." self.af[5]=".5BA2." self.af[6]=" 43 " self.af[7]=" .. " self.movedataset() #b self.bf[0]=" 9A " self.bf[1]=" CB " self.bf[2]=" 12 " self.bf[3]=" .. " self.bf[4]="8....3" self.bf[5]="7....4" self.bf[6]=" .. " self.bf[7]=" 65 " self.af[0]=" C9 " self.af[1]=" BA " self.af[2]=" 34 " self.af[3]=" .. " self.af[4]="2....5" self.af[5]="1....6" self.af[6]=" .. " self.af[7]=" 87 " self.movedataset() #u self.bf[0]=" .. " self.bf[1]=" 12 " self.bf[2]=" 9A " self.bf[3]=" CB " self.bf[4]="876543" self.bf[5]="......" self.bf[6]=" .. " self.bf[7]=" .. " self.af[0]=" .. " self.af[1]=" 78 " self.af[2]=" C9 " self.af[3]=" BA " self.af[4]="654321" self.af[5]="......" self.af[6]=" .. " self.af[7]=" .. " self.movedataset() #d self.bf[0]=" 87 " self.bf[1]=" .. " self.bf[2]=" .. " self.bf[3]=" .. " self.bf[4]="......" self.bf[5]="123456" self.bf[6]=" 9A " self.bf[7]=" CB " self.af[0]=" 65 " self.af[1]=" .. " self.af[2]=" .. " self.af[3]=" .. " self.af[4]="......" self.af[5]="781234" self.af[6]=" C9 " self.af[7]=" BA " self.movedataset() q=Cube() # self.bd+=" BB " # self.bd+=" BB " # self.bd+=" UU " # self.bd+=" UU " # self.bd+="LLFFRR" # self.bd+="LLFFRR" # self.bd+=" DD " # self.bd+=" DD " # "rlfbud" for i in range(10000): q.randmove() q.tc=0 st=time.time() sts=0 while True: if sts==0: q.randmove() if q.pmatch([(3,3,"U"),(3,4,"F"),(4,4,"R")]): sts=1 print("sts1") q.write() elif sts==1: q.rangemove([1,3,5]) if q.pmatch([(2,3,"U"),(2,4,"F"),(1,4,"L")]): sts=2 print("sts2") q.write() elif sts==2: q.rangemove([3,5]) if q.pmatch([(2,2,"U"),(0,4,"L")]): sts=3 print("sts3") q.write() elif sts==3: sw=0 q.store() q.seq("dduu dldddlll") if q.pmatch([(2,2,"U"),(2,3,"U"),(3,2,"U"),(3,3,"U")]):sw=1 if sw==0: q.back() q.seq("uddd dddrrrdr") if q.pmatch([(2,2,"U"),(2,3,"U"),(3,2,"U"),(3,3,"U")]):sw=1 if sw==0: q.back() q.seq("dduu ldddlllfff dddf") if q.pmatch([(2,2,"U"),(2,3,"U"),(3,2,"U"),(3,3,"U")]):sw=1 if sw==0: q.back() q.seq("dddu rrrdrf dfff") if q.pmatch([(2,2,"U"),(2,3,"U"),(3,2,"U"),(3,3,"U")]):sw=1 if sw==0: q.back() q.seq("dduu fffrrrddrf") if q.pmatch([(2,2,"U"),(2,3,"U"),(3,2,"U"),(3,3,"U")]):sw=1 if sw==1: sts=4 print("sts4") q.write() else: q.back() print("sts0") sts=0 elif sts==4: q.seq("llrr") for i in range(4): if q.pmatch([(2,2,"D"),(2,3,"D"),(3,4,"D")]): q.seqD() sts=5 print("sts5") q.write() break if q.pmatch([(2,2,"D"),(3,3,"D"),(2,4,"D")]): q.seqE() sts=5 print("sts5") q.write() break q.turn(4) if sts==4: print("sts0") sts=0 elif sts==5: if q.bd[8]!=q.bd[9] and q.bd[30]!=q.bd[31] and q.bd[32]!=q.bd[33] and q.bd[34]!=q.bd[35]: q.seqE() q.seq("uudd") q.seqD() sts=6 print("sts6") q.write() else: print("sts0") sts=0 elif sts==6: q.turn(4) else: q.randmove() if q.countface()>=3: q.write() print("") if q.countface()==6: break print(str(time.time()-st))