python三阶魔方_三阶魔方自动求解及动态可视化matlab代码

三阶魔方自动求解及动态可视化matlab代码

思路与步骤

三阶魔方有6个面,每个面有

3

×

3

3\times3

3×3小块,用一个

6

×

3

×

3

6\times3\times3

6×3×3的矩阵来保存魔方的状态;

定义4+12种魔方旋转行为:整体旋转(左右上下共4种方式AaCc),侧面(6个侧面顺逆时针旋转共12种方式LlRrUuDdFfBb),旋转可视化界面方便调试(三维小面作图fill3创建

6

×

3

×

3

=

54

6\times3\times3=54

6×3×3=54个小面对象,对象成员属性包括颜色和4个三维坐标点,定义了全局函数对小面进行绕轴旋转操作,如此可以可视化调试,确保程序是按人想法运行),旋转行为改变矩阵的状态矩阵;

定义随机打乱和逆序恢复的函数,测试确保人为给出旋转公式,魔方能正确旋转;

编写按公式恢复魔方的函数,记录恢复过程的旋转过程公式(取一个魔方一边拧一边编程,考虑所有情况)

1):顶层拼十字:简化为4次恢复顶面棱中小块(不破坏已恢复的结果)

2):顶层拼四角:简化为4次恢复顶面的角小块(不破坏已恢复的结果)

第一层恢复完成,剩下可按公式恢复魔方,把第一层置底

3):二层拼棱角:简化为4次恢复二层棱角小块(不破坏已恢复的结果)

第二层恢复完成

4):按公式顶层拼十字

5):按公式顶层四角对应恢复

6):按公式顶面同色恢复

7):按公式顶面棱边恢复

第三层恢复完成

编写一个公式简化的函数,如消除相邻正反操作、消除4次相同操作、3次同向旋转替换为一次反向旋转操作、不相关跳跃旋转整合等,未能考虑所有可化简的情况,还未能化简为最少旋转步数公式。时间精力有限,待以后有新思路的时候再玩。

总结

很多指令都是现查现用的,编程模块化,过程分解,编程周期较长,编完以后,整体思路清晰,但单独模块缺少注释,不好看懂,特别是手动旋转对应编程部分,时间久了容易忘记。这是2019年1月编的代码(忘记了具体参考过哪些代码了),现在整理下思路,这里三阶魔方恢复算法是普通的按公式还原算法,主要难点在于判断魔方的状态,我编程的过程中是实际拿着个魔方一边转一边进行状态观察遍历,方法比较笨,可以实现自动魔方还原,但是还原步骤较多,程序中通过增加一些函数进行了步骤的简化

function TurnManu=simplifyTurnManu(TurnManu)

但是简化结果并不彻底,需要更高级的简化函数,或高级的魔方恢复算法。

思考

如果能够用快速相机记录并识别魔方高手旋转魔方的方法,通过机器学习、聚类、迁移等思想,改善算法,数据即算法?如果没有如果,给定初始的算法,能不能让算法自己通过运行自学习,更新算法,或者产生模拟高级的数据用于机器学习?感觉这会不会是一个很好的案例?数据为王?算法为王?Alpha Zero从0开始?有人实现了吗?

参考链接

没作记录,忘了,对参考过代码的博主表示歉意,如有发现雷同的地方,欢迎提醒,以作补充。

294ff81566d69c7ff188100bf1df4ef4.png

源代码

%% 主调函数

% ---定义魔方的初始状态

face0=ones(3,3);

MFstate={face0;face0+1;face0+2;face0+3;face0+4;face0+5};

% % MFstate=faceTurn(MFstate,'r');

% % MFstate=sideTurn(MFstate,'r');

[MFstate,TurnManu]=disorganize(MFstate,10);

% DispTurnManu(TurnManu,1);

figure;pltMoFang(MFstate);

set(gcf,'color','k');axis square;axis off;view(160,30)

TurnManu=simplifyTurnManu(TurnManu,0);

TurnManu0=DispTurnManu(TurnManu);

disp({'魔方打乱操作:';TurnManu0.'})

% MFstate=disorganize(MFstate,TurnManu);

[MFstate1,TurnManu1]=InvRecover(MFstate,TurnManu);

TurnManu0=DispTurnManu(TurnManu1);

disp({'魔方逆序恢复操作:';TurnManu0.'})

figure;pltMoFang(MFstate1);

set(gcf,'color','k');axis square;axis off;view(160,30)

[MFstate,TurnManu2]=formulaRecover(MFstate);

TurnManu2=simplifyTurnManu(TurnManu2,0);

TurnManu0=DispTurnManu(TurnManu2);

disp({'魔方公式恢复操作:';TurnManu0.'})

figure;pltMoFang(MFstate);

set(gcf,'color','k');axis square;axis off;view(160,30)

-----------------------------------------------------------------------------------------------------------------------------------

%% 动画显示

face0=ones(3,3);

MFstate={face0;face0+1;face0+2;face0+3;face0+4;face0+5};

limL=-4;limU=1;

close all;fig=figure; fig.Color=[1,1,1]; fig.Position=[400 200 550 600];

fig.ToolBar='none'; fig.MenuBar='none'; fig.NumberTitle='off'; fig.Name='魔方';

hds=pltMoFang(MFstate);

set(gcf,'color','k');axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);

pause(4);

TurnManu_all=[TurnManu;TurnManu2];

for n=1:length(TurnManu_all)

pause(0.00001);

turnAroundP(hds,TurnManu_all(n),10,0.01,limL,limU);

MFstate=disorganize(MFstate,TurnManu_all(n));

hold off;hds=pltMoFang(MFstate);axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);

end

%% 部分子函数编写

%% 公式恢复魔方

function [MFstate,TurnManu]=formulaRecover(MFstate)

TurnManu=[];

%---第一层恢复

%将顶层拼十字

num=zeros(1,4);

for n=1:4

[MFstate,TnMn,num]=recoverF10(MFstate,num);TurnManu=[TurnManu;TnMn];

if sum(num)==4;break;end

MFstate=faceTurn(MFstate,'r');num=circshift(num,-1);TurnManu=[TurnManu;'a'];

end

TurnManu=[TurnManu;('- ').'];

%将顶层4个角恢复,完成第一层恢复

num=zeros(1,4);

for n=1:4

[MFstate,TnMn,num]=recoverF4(MFstate,num);TurnManu=[TurnManu;TnMn];

if sum(num)==4;break;end

MFstate=faceTurn(MFstate,'r');num=circshift(num,-1);TurnManu=[TurnManu;'a'];

end

TurnManu=[TurnManu;('- ').'];

%第二层恢复 4个角恢复,完成第二层恢复

[MFstate,TnMn]=disorganize(MFstate,'CC');TurnManu=[TurnManu;TnMn];

num=zeros(1,4);

for n=1:4

[MFstate,TnMn,num]=recover2F4(MFstate,num);TurnManu=[TurnManu;TnMn];

if sum(num)==4;break;end

MFstate=faceTurn(MFstate,'l');num=circshift(num,-1);TurnManu=[TurnManu;'A'];

end

TurnManu=[TurnManu;('- ').'];

%第三层公式恢复

%---步骤1:顶上拼十字

[MFstate,TnMn]=recover3F10(MFstate);TurnManu=[TurnManu;TnMn];

TurnManu=[TurnManu;('- ').'];

%---步骤2:四个角对应上

[MFstate,TnMn]=recover3F4(MFstate);TurnManu=[TurnManu;TnMn];

TurnManu=[TurnManu;('- ').'];

%---步骤3:顶面恢复

[MFstate,TnMn]=recover3Face(MFstate);TurnManu=[TurnManu;TnMn];

while isempty(TnMn)==0

[MFstate,TnMn]=recover3Face(MFstate);TurnManu=[TurnManu;TnMn];

end

TurnManu=[TurnManu;('- ').'];

%---步骤4:顶棱中恢复

[MFstate,TnMn]=recover3FArris(MFstate);TurnManu=[TurnManu;TnMn];

while isempty(TnMn)==0

[MFstate,TnMn]=recover3FArris(MFstate);TurnManu=[TurnManu;TnMn];

end

end

% ---定义魔方整体旋转行为---改变朝前面

% r,l,u,d 共4种行为

function MFstate=faceTurn(MFstate,manu)

if manu=='r'

MFstate={MFstate{5};rot90(MFstate{3},2);MFstate{1};fliplr(MFstate{4}.');rot90(MFstate{2},2);fliplr(MFstate{6}).'};

elseif manu=='l'

MFstate={MFstate{3};rot90(MFstate{5},2);rot90(MFstate{2},2);fliplr(MFstate{4}).';MFstate{1};fliplr(MFstate{6}.')};

elseif manu=='u'

MFstate={MFstate{4};MFstate{6};fliplr(MFstate{3}.');MFstate{2};fliplr(MFstate{5}).';MFstate{1}};

elseif manu=='d'

MFstate={MFstate{6};MFstate{4};fliplr(MFstate{3}).';MFstate{1};fliplr(MFstate{5}.');MFstate{2}};

else

return;

end

end

----------------------------------------------------------------------------------------------------------------------------------------------------------

% ---定义一个旋转过程的函数

function turnAroundP(hds,TurnManu,stepNum,tSec,limL,limU)

if nargin==4

limL=-3;limU=0;

end

if TurnManu=='a'

[az,el]=view();

azStep=linspace(0,-90,stepNum);

for n=1:stepNum

view(az+azStep(n),el);pause(tSec);

end

elseif TurnManu=='A'

[az,el]=view();

azStep=linspace(0,90,stepNum);

for n=1:stepNum

view(az+azStep(n),el);pause(tSec);

end

elseif TurnManu=='c' || TurnManu=='C'

azStep=90/(stepNum+1);

if TurnManu=='C'

azStep=-azStep;

end

direct=[1,0,0];

origin=-[1.5,1.5,1.5];

for n=1:stepNum

for k=1:6

for kk=1:3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);pause(tSec);

end

elseif TurnManu=='r' || TurnManu=='R'

azStep=90/(stepNum+1);

if TurnManu=='R'

azStep=-azStep;

end

direct=[1,0,0];

origin=-[1.5,1.5,1.5];

for n=1:stepNum

for k=3

for kk=1:3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for k=[1,2,4,6]

for kk=1:3

for kkk=3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);pause(tSec);

end

elseif TurnManu=='l' || TurnManu=='L'

azStep=-90/(stepNum+1);

if TurnManu=='L'

azStep=-azStep;

end

direct=[1,0,0];

origin=-[1.5,1.5,1.5];

for n=1:stepNum

for k=5

for kk=1:3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for k=[1,2,4,6]

for kk=1:3

for kkk=1

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);pause(tSec);

end

elseif TurnManu=='u' || TurnManu=='U'

azStep=-90/(stepNum+1);

if TurnManu=='U'

azStep=-azStep;

end

direct=[0,0,1];

origin=-[1.5,1.5,1.5];

for n=1:stepNum

for k=6

for kk=1:3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for k=[1,3,5]

for kk=1

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for k=2

for kk=3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);pause(tSec);

end

elseif TurnManu=='d' || TurnManu=='D'

azStep=90/(stepNum+1);

if TurnManu=='D'

azStep=-azStep;

end

direct=[0,0,1];

origin=-[1.5,1.5,1.5];

for n=1:stepNum

for k=4

for kk=1:3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for k=[1,3,5]

for kk=3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for k=2

for kk=1

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);pause(tSec);

end

elseif TurnManu=='f' || TurnManu=='F'

azStep=-90/(stepNum+1);

if TurnManu=='F'

azStep=-azStep;

end

direct=[0,1,0];

origin=-[1.5,1.5,1.5];

for n=1:stepNum

for k=1

for kk=1:3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for kk=1:3

h=hds{3}{kk,1};

rotate(h,direct,azStep,origin);

end

for kk=1:3

h=hds{5}{kk,3};

rotate(h,direct,azStep,origin);

end

for kkk=1:3

h=hds{4}{1,kkk};

rotate(h,direct,azStep,origin);

end

for kkk=1:3

h=hds{6}{3,kkk};

rotate(h,direct,azStep,origin);

end

axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);pause(tSec);

end

elseif TurnManu=='b' || TurnManu=='B'

azStep=90/(stepNum+1);

if TurnManu=='B'

azStep=-azStep;

end

direct=[0,1,0];

origin=-[1.5,1.5,1.5];

for n=1:stepNum

for k=2

for kk=1:3

for kkk=1:3

h=hds{k}{kk,kkk};

rotate(h,direct,azStep,origin);

end

end

end

for kk=1:3

h=hds{3}{kk,3};

rotate(h,direct,azStep,origin);

end

for kk=1:3

h=hds{5}{kk,1};

rotate(h,direct,azStep,origin);

end

for kkk=1:3

h=hds{4}{3,kkk};

rotate(h,direct,azStep,origin);

end

for kkk=1:3

h=hds{6}{1,kkk};

rotate(h,direct,azStep,origin);

end

axis([limL,limU,limL,limU,limL,limU]);axis off;view(160,30);pause(tSec);

end

end

end

% ---定义魔方整体旋转行为---改变朝前面

% r,l,u,d 共4种行为

function MFstate=faceTurn(MFstate,manu)

if manu=='r'

MFstate={MFstate{5};rot90(MFstate{3},2);MFstate{1};fliplr(MFstate{4}.');rot90(MFstate{2},2);fliplr(MFstate{6}).'};

elseif manu=='l'

MFstate={MFstate{3};rot90(MFstate{5},2);rot90(MFstate{2},2);fliplr(MFstate{4}).';MFstate{1};fliplr(MFstate{6}.')};

elseif manu=='u'

MFstate={MFstate{4};MFstate{6};fliplr(MFstate{3}.');MFstate{2};fliplr(MFstate{5}).';MFstate{1}};

elseif manu=='d'

MFstate={MFstate{6};MFstate{4};fliplr(MFstate{3}).';MFstate{1};fliplr(MFstate{5}.');MFstate{2}};

else

return;

end

end

% ---定义魔方侧边旋转行为(顺时针:fliplr(~.') or rot90(~,-1)逆时针:fliplr(~).' or rot90(~,1))

% rR,lL,uU,dD,fF,bB共12种行为(可进一步定义连续2步行为:R2,L2,U2,D2,F2,B2)

function MFstate=sideTurn(MFstate,manu)

if manu=='r'

MFstate{3}=fliplr(MFstate{3}.');

tmp=MFstate{1}(:,3);

MFstate{1}(:,3)=MFstate{4}(:,3);

MFstate{4}(:,3)=MFstate{2}(:,3);

MFstate{2}(:,3)=MFstate{6}(:,3);

MFstate{6}(:,3)=tmp;

elseif manu=='R'

MFstate{3}=fliplr(MFstate{3}).';

tmp=MFstate{1}(:,3);

MFstate{1}(:,3)=MFstate{6}(:,3);

MFstate{6}(:,3)=MFstate{2}(:,3);

MFstate{2}(:,3)=MFstate{4}(:,3);

MFstate{4}(:,3)=tmp;

elseif manu=='l'

MFstate{5}=fliplr(MFstate{5}.');

tmp=MFstate{1}(:,1);

MFstate{1}(:,1)=MFstate{6}(:,1);

MFstate{6}(:,1)=MFstate{2}(:,1);

MFstate{2}(:,1)=MFstate{4}(:,1);

MFstate{4}(:,1)=tmp;

elseif manu=='L'

MFstate{5}=fliplr(MFstate{5}).';

tmp=MFstate{1}(:,1);

MFstate{1}(:,1)=MFstate{4}(:,1);

MFstate{4}(:,1)=MFstate{2}(:,1);

MFstate{2}(:,1)=MFstate{6}(:,1);

MFstate{6}(:,1)=tmp;

elseif manu=='u'

MFstate{6}=fliplr(MFstate{6}.');

tmp=MFstate{1}(1,:);

MFstate{1}(1,:)=MFstate{3}(1,:);

MFstate{3}(1,:)=fliplr(MFstate{2}(3,:));

MFstate{2}(3,:)=fliplr(MFstate{5}(1,:));

MFstate{5}(1,:)=tmp;

elseif manu=='U'

MFstate{6}=fliplr(MFstate{6}).';

tmp=MFstate{1}(1,:);

MFstate{1}(1,:)=MFstate{5}(1,:);

MFstate{5}(1,:)=fliplr(MFstate{2}(3,:));

MFstate{2}(3,:)=fliplr(MFstate{3}(1,:));

MFstate{3}(1,:)=tmp;

elseif manu=='d'

MFstate{4}=fliplr(MFstate{4}.');

tmp=MFstate{1}(3,:);

MFstate{1}(3,:)=MFstate{5}(3,:);

MFstate{5}(3,:)=fliplr(MFstate{2}(1,:));

MFstate{2}(1,:)=fliplr(MFstate{3}(3,:));

MFstate{3}(3,:)=tmp;

elseif manu=='D'

MFstate{4}=fliplr(MFstate{4}).';

tmp=MFstate{1}(3,:);

MFstate{1}(3,:)=MFstate{3}(3,:);

MFstate{3}(3,:)=fliplr(MFstate{2}(1,:));

MFstate{2}(1,:)=fliplr(MFstate{5}(3,:));

MFstate{5}(3,:)=tmp;

elseif manu=='f'

MFstate{1}=fliplr(MFstate{1}.');

tmp=MFstate{6}(3,:);

MFstate{6}(3,:)=fliplr(MFstate{5}(:,3).');

MFstate{5}(:,3)=MFstate{4}(1,:).';

MFstate{4}(1,:)=fliplr(MFstate{3}(:,1).');

MFstate{3}(:,1)=tmp.';

elseif manu=='F'

MFstate{1}=fliplr(MFstate{1}).';

tmp=MFstate{6}(3,:);

MFstate{6}(3,:)=MFstate{3}(:,1).';

MFstate{3}(:,1)=fliplr(MFstate{4}(1,:)).';

MFstate{4}(1,:)=MFstate{5}(:,3).';

MFstate{5}(:,3)=fliplr(tmp).';

elseif manu=='b'

MFstate{2}=fliplr(MFstate{2}.');

tmp=MFstate{6}(1,:);

MFstate{6}(1,:)=MFstate{3}(:,3).';

MFstate{3}(:,3)=fliplr(MFstate{4}(3,:)).';

MFstate{4}(3,:)=MFstate{5}(:,1).';

MFstate{5}(:,1)=fliplr(tmp).';

elseif manu=='B'

MFstate{2}=fliplr(MFstate{2}).';

tmp=MFstate{6}(1,:);

MFstate{6}(1,:)=fliplr(MFstate{5}(:,1).');

MFstate{5}(:,1)=MFstate{4}(3,:).';

MFstate{4}(3,:)=fliplr(MFstate{3}(:,3).');

MFstate{3}(:,3)=tmp.';

else

return;

end

end

% ---作魔方正方体图形

function hds=pltMoFang(MFstate)

for n=1:6

h=pltMoFangface(MFstate{n},n);

hds{n}=h;

end

end

function hds=pltMoFangface(A,Fnum)

if Fnum==1

x=repmat(0:1:3,4,1);

y=zeros(4);

z=repmat(0:1:3,4,1).';

elseif Fnum==2

x=repmat(0:1:3,4,1);

y=3*ones(4);

z=repmat(3:-1:0,4,1).';

elseif Fnum==3

x=3*ones(4);

y=repmat(0:1:3,4,1);

z=repmat(0:1:3,4,1).';

elseif Fnum==4

x=repmat(0:1:3,4,1);

y=repmat(0:1:3,4,1).';

z=3*ones(4);

elseif Fnum==5

x=zeros(4);

y=repmat(3:-1:0,4,1);

z=repmat(0:1:3,4,1).';

elseif Fnum==6

x=repmat(0:1:3,4,1);

y=repmat(3:-1:0,4,1).';

z=zeros(4);

end

for n=1:3

for nn=1:3

X=-[x(n,nn),x(n+1,nn),x(n+1,nn+1),x(n,nn+1)];

Y=-[y(n,nn),y(n+1,nn),y(n+1,nn+1),y(n,nn+1)];

Z=-[z(n,nn),z(n+1,nn),z(n+1,nn+1),z(n,nn+1)];

if A(n,nn)==1

C=[1,0,0];

elseif A(n,nn)==2

C=[1,0,1];

elseif A(n,nn)==3

C=[1,1,0];

elseif A(n,nn)==4

C=[0,1,0];

elseif A(n,nn)==5

C=[1,1,1];

elseif A(n,nn)==6

C=[0,0,1];

end

hds{n,nn}=fill3(X,Y,Z,C);hold on;

end

end

end

%% 随机打乱魔方的函数

function [MFstate,TurnManu]=disorganize(MFstate,TurnNum)

% % r,l,u,d 共4种行为--->更名a,A,d,D

% MFstate=faceTurn(MFstate,'N');

% % rR,lL,uU,dD,fF,bB共12种行为

% MFstate=sideTurn(MFstate,'N');

if ischar(TurnNum)==1

TurnManu=TurnNum;

TurnNum=length(TurnManu);

if size(TurnManu,2)>1

TurnManu=TurnManu.';

end

if contains(TurnManu.','2')==1

TurnManu=DispTurnManu(TurnManu);

end

else

TurnManu=char(TurnNum,1);

flagStr=['a','A','c','C','r','R','l','L','u','U','d','D','f','F','b','B'];

for n=1:TurnNum

num=randperm(16,1);

TurnManu(n)=flagStr(num);

end

end

for n=1:TurnNum

flag=TurnManu(n);

switch flag

case 'a'

MFstate=faceTurn(MFstate,'r');

case 'A'

MFstate=faceTurn(MFstate,'l');

case 'c'

MFstate=faceTurn(MFstate,'u');

case 'C'

MFstate=faceTurn(MFstate,'d');

case 'r'

MFstate=sideTurn(MFstate,'r');

case 'R'

MFstate=sideTurn(MFstate,'R');

case 'l'

MFstate=sideTurn(MFstate,'l');

case 'L'

MFstate=sideTurn(MFstate,'L');

case 'u'

MFstate=sideTurn(MFstate,'u');

case 'U'

MFstate=sideTurn(MFstate,'U');

case 'd'

MFstate=sideTurn(MFstate,'d');

case 'D'

MFstate=sideTurn(MFstate,'D');

case 'f'

MFstate=sideTurn(MFstate,'f');

case 'F'

MFstate=sideTurn(MFstate,'F');

case 'b'

MFstate=sideTurn(MFstate,'b');

case 'B'

MFstate=sideTurn(MFstate,'B');

end

end

end

%% 逆序恢复魔方

function [MFstate,TurnManu]=InvRecover(MFstate,TurnManu)

% % r,l,u,d 共4种行为--->更名a,A,c,C

% MFstate=faceTurn(MFstate,'N');

% % rR,lL,uU,dD,fF,bB共12种行为

% MFstate=sideTurn(MFstate,'N');

TurnManu=flipud(TurnManu);

for n=1:length(TurnManu)

flag=TurnManu(n);

switch flag

case 'a'

MFstate=faceTurn(MFstate,'l');

TurnManu(n)='A';

case 'A'

MFstate=faceTurn(MFstate,'r');

TurnManu(n)='a';

case 'c'

MFstate=faceTurn(MFstate,'d');

TurnManu(n)='C';

case 'C'

MFstate=faceTurn(MFstate,'u');

TurnManu(n)='c';

case 'r'

MFstate=sideTurn(MFstate,'R');

TurnManu(n)='R';

case 'R'

MFstate=sideTurn(MFstate,'r');

TurnManu(n)='r';

case 'l'

MFstate=sideTurn(MFstate,'L');

TurnManu(n)='L';

case 'L'

MFstate=sideTurn(MFstate,'l');

TurnManu(n)='l';

case 'u'

MFstate=sideTurn(MFstate,'U');

TurnManu(n)='U';

case 'U'

MFstate=sideTurn(MFstate,'u');

TurnManu(n)='u';

case 'd'

MFstate=sideTurn(MFstate,'D');

TurnManu(n)='D';

case 'D'

MFstate=sideTurn(MFstate,'d');

TurnManu(n)='d';

case 'f'

MFstate=sideTurn(MFstate,'F');

TurnManu(n)='F';

case 'F'

MFstate=sideTurn(MFstate,'f');

TurnManu(n)='f';

case 'b'

MFstate=sideTurn(MFstate,'B');

TurnManu(n)='B';

case 'B'

MFstate=sideTurn(MFstate,'b');

TurnManu(n)='b';

end

end

end

% %% 简化旋转魔方操作,只取有效操作

% function TurnManu=simplifyTurnManu(TurnManu)

% N0=length(TurnManu);

%

% % ---消除正反操作

% N=length(TurnManu);

% n=N;

% while(n>1)

% if (TurnManu(n)=='a' && TurnManu(n-1)=='A' ) || (TurnManu(n)=='A' && TurnManu(n-1)=='a' ) || ...

% (TurnManu(n)=='c' && TurnManu(n-1)=='C' ) || (TurnManu(n)=='C' && TurnManu(n-1)=='c' ) || ...

% (TurnManu(n)=='r' && TurnManu(n-1)=='R' ) || (TurnManu(n)=='R' && TurnManu(n-1)=='r' ) ||...

% (TurnManu(n)=='l' && TurnManu(n-1)=='L' ) || (TurnManu(n)=='L' && TurnManu(n-1)=='l' ) ||...

% (TurnManu(n)=='u' && TurnManu(n-1)=='U' ) || (TurnManu(n)=='U' && TurnManu(n-1)=='u' ) ||...

% (TurnManu(n)=='d' && TurnManu(n-1)=='D' ) || (TurnManu(n)=='D' && TurnManu(n-1)=='d' ) ||...

% (TurnManu(n)=='f' && TurnManu(n-1)=='F' ) || (TurnManu(n)=='F' && TurnManu(n-1)=='f' ) ||...

% (TurnManu(n)=='b' && TurnManu(n-1)=='B' ) || (TurnManu(n)=='B' && TurnManu(n-1)=='b' )

%

% TurnManu(n-1:n)=[];

% n=n-1;

% end

% n=n-1;

% end

%

%

% % ---消除4次相同操作

% N=length(TurnManu);

% n=N;

% while(n>3)

% if (TurnManu(n)==TurnManu(n-1)) &&...

% (TurnManu(n-1)==TurnManu(n-2)) &&...

% (TurnManu(n-2)==TurnManu(n-3))

%

% TurnManu(n-3:n)=[];

% n=n-3;

% end

% n=n-1;

% end

%

% % ---3次相同操作替换为一次反向操作

% N=length(TurnManu);

% n=N;

% while(n>2)

% if (TurnManu(n)==TurnManu(n-1)) &&...

% (TurnManu(n-1)==TurnManu(n-2))

%

% if TurnManu(n-2)>90

% TurnManu(n-2)=TurnManu(n-2)-32;

% else

% TurnManu(n-2)=TurnManu(n-2)+32;

% end

%

% TurnManu(n-1:n)=[];

% n=n-1;

%

% end

% n=n-1;

% end

%

% while(length(TurnManu)

% TurnManu=simplifyTurnManu(TurnManu);

% N0=length(TurnManu);

% end

%

% end

%% 简化显示旋转魔方操作,将重复2次操作显示为操作2

function TurnManu=DispTurnManu(TurnManu,dispFlag)

if nargin==1

dispFlag=0;

end

if size(TurnManu,2)>1

TurnManu=TurnManu.';

end

if contains(TurnManu.','2')

if dispFlag==1

disp(['Before: ',TurnManu.'])

end

N=length(TurnManu);

n=N;

while(n>1)

if TurnManu(n)=='2'

TurnManu(n)=TurnManu(n-1);

end

n=n-1;

end

if dispFlag==1

disp(['After: ',TurnManu.'])

end

else

if dispFlag==1

disp(['Before: ',TurnManu.'])

end

N=length(TurnManu);

n=N;

while(n>1)

if TurnManu(n)==TurnManu(n-1)

num=2;

while n>2 && TurnManu(n)==TurnManu(n-2)

num=num+1;

TurnManu(n)=[];

n=n-1;

end

TurnManu(n)=num2str(num);

end

n=n-1;

end

if dispFlag==1

disp(['After: ',TurnManu.'])

end

end

end

%% 将顶层4个角恢复

function [MFstate,TurnManu,num]=recoverF4(MFstate,num)

TurnManu=[];

%---第一层恢复

%记录面的位置

obj1=MFstate{1}(2,2);obj2=MFstate{2}(2,2);

obj3=MFstate{3}(2,2);obj4=MFstate{4}(2,2);

obj5=MFstate{5}(2,2);obj6=MFstate{6}(2,2);

if MFstate{6}(1,1)==obj6 && MFstate{2}(3,1)==obj2 && MFstate{5}(1,1)==obj5

num(1)=1; end

if MFstate{6}(1,3)==obj6 && MFstate{2}(3,3)==obj2 && MFstate{3}(1,3)==obj3

num(2)=1; end

if MFstate{6}(3,3)==obj6 && MFstate{1}(1,3)==obj1 && MFstate{3}(1,1)==obj3

num(3)=1; end

if MFstate{6}(3,1)==obj6 && MFstate{1}(1,1)==obj1 && MFstate{5}(1,3)==obj5

num(4)=1; end

%---4角拼完

if sum(num)==4; return;end

if num(1)==0

if MFstate{2}(1,1)==obj2 && MFstate{5}(3,1)==obj6 && MFstate{4}(3,1)==obj5

[MFstate,TnMn]=disorganize(MFstate,'BlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(1,1)==obj6 && MFstate{5}(3,1)==obj5 && MFstate{4}(3,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'lBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(1,1)==obj5 && MFstate{5}(3,1)==obj2 && MFstate{4}(3,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'Ld2ldBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(3,3)==obj2 && MFstate{2}(1,3)==obj6 && MFstate{4}(3,3)==obj5

[MFstate,TnMn]=disorganize(MFstate,'dBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(3,3)==obj6 && MFstate{2}(1,3)==obj5 && MFstate{4}(3,3)==obj2

[MFstate,TnMn]=disorganize(MFstate,'dlBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(3,3)==obj5 && MFstate{2}(1,3)==obj2 && MFstate{4}(3,3)==obj6

[MFstate,TnMn]=disorganize(MFstate,'dLd2ldBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(3,3)==obj2 && MFstate{1}(3,1)==obj6 && MFstate{4}(1,1)==obj5

[MFstate,TnMn]=disorganize(MFstate,'DBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(3,3)==obj6 && MFstate{1}(3,1)==obj5 && MFstate{4}(1,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'DlBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(3,3)==obj5 && MFstate{1}(3,1)==obj2 && MFstate{4}(1,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'DLd2ldBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(3,3)==obj2 && MFstate{3}(3,1)==obj6 && MFstate{4}(1,3)==obj5

[MFstate,TnMn]=disorganize(MFstate,'D2BlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(3,3)==obj6 && MFstate{3}(3,1)==obj5 && MFstate{4}(1,3)==obj2

[MFstate,TnMn]=disorganize(MFstate,'D2lBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(3,3)==obj5 && MFstate{3}(3,1)==obj2 && MFstate{4}(1,3)==obj6

[MFstate,TnMn]=disorganize(MFstate,'D2Ld2ldBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,1)==obj2 && MFstate{2}(3,1)==obj6 && MFstate{6}(1,1)==obj5

[MFstate,TnMn]=disorganize(MFstate,'Ldl2BLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,1)==obj6 && MFstate{2}(3,1)==obj5 && MFstate{6}(1,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'bDB2lbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(1,3)==obj2 && MFstate{6}(1,3)==obj6 && MFstate{2}(3,3)==obj5

[MFstate,TnMn]=disorganize(MFstate,'rdRBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(1,3)==obj6 && MFstate{6}(1,3)==obj5 && MFstate{2}(3,3)==obj2

[MFstate,TnMn]=disorganize(MFstate,'rdRlBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(1,3)==obj5 && MFstate{6}(1,3)==obj2 && MFstate{2}(3,3)==obj6

[MFstate,TnMn]=disorganize(MFstate,'rDRdBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,3)==obj2 && MFstate{6}(3,1)==obj6 && MFstate{1}(1,1)==obj5

[MFstate,TnMn]=disorganize(MFstate,'lDLDlBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,3)==obj6 && MFstate{6}(3,1)==obj5 && MFstate{1}(1,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'lD2LdlBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,3)==obj5 && MFstate{6}(3,1)==obj2 && MFstate{1}(1,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'lDLDBlbL');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(1,3)==obj2 && MFstate{6}(3,3)==obj6 && MFstate{3}(1,1)==obj5

[MFstate,TnMn]=disorganize(MFstate,'RD2rlBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(1,3)==obj6 && MFstate{6}(3,3)==obj5 && MFstate{3}(1,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'RdrD2lBLb');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(1,3)==obj5 && MFstate{6}(3,3)==obj2 && MFstate{3}(1,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'RD2rBlbL');TurnManu=[TurnManu;TnMn];

end

% 恢复完成

num(1)=1;

end

end

%% 公式恢复第一层的十字

function [MFstate,TurnManu,num]=recoverF10(MFstate,num)

TurnManu=[];

%---第一层恢复

%记录面的位置

obj1=MFstate{1}(2,2);obj2=MFstate{2}(2,2);

obj3=MFstate{3}(2,2);obj4=MFstate{4}(2,2);

obj5=MFstate{5}(2,2);obj6=MFstate{6}(2,2);

if sum(num)==0

%将顶层拼十字

%---case1:顶层有同色块

if MFstate{6}(1,2)==obj6 && MFstate{2}(3,2)==obj2

elseif MFstate{6}(1,2)==obj6 && MFstate{2}(3,2)==obj3

[MFstate,TnMn]=disorganize(MFstate,'u');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(1,2)==obj6 && MFstate{2}(3,2)==obj1

[MFstate,TnMn]=disorganize(MFstate,'u2');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(1,2)==obj6 && MFstate{2}(3,2)==obj5

[MFstate,TnMn]=disorganize(MFstate,'U');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(2,3)==obj6 && MFstate{3}(1,2)==obj3

elseif MFstate{6}(2,3)==obj6 && MFstate{3}(1,2)==obj1

[MFstate,TnMn]=disorganize(MFstate,'u');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(2,3)==obj6 && MFstate{3}(1,2)==obj5

[MFstate,TnMn]=disorganize(MFstate,'u2');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(2,3)==obj6 && MFstate{3}(1,2)==obj2

[MFstate,TnMn]=disorganize(MFstate,'U');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(3,2)==obj6 && MFstate{1}(1,2)==obj1

elseif MFstate{6}(3,2)==obj6 && MFstate{1}(1,2)==obj5

[MFstate,TnMn]=disorganize(MFstate,'u');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(3,2)==obj6 && MFstate{1}(1,2)==obj2

[MFstate,TnMn]=disorganize(MFstate,'u2');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(3,2)==obj6 && MFstate{1}(1,2)==obj3

[MFstate,TnMn]=disorganize(MFstate,'U');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(2,1)==obj6 && MFstate{5}(1,2)==obj5

elseif MFstate{6}(2,1)==obj6 && MFstate{5}(1,2)==obj2

[MFstate,TnMn]=disorganize(MFstate,'u');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(2,1)==obj6 && MFstate{5}(1,2)==obj3

[MFstate,TnMn]=disorganize(MFstate,'u2');TurnManu=[TurnManu;TnMn];

elseif MFstate{6}(2,1)==obj6 && MFstate{5}(1,2)==obj1

[MFstate,TnMn]=disorganize(MFstate,'U');TurnManu=[TurnManu;TnMn];

end

end

if MFstate{6}(1,2)==obj6 && MFstate{2}(3,2)==obj2

num(1)=1; end

if MFstate{6}(2,3)==obj6 && MFstate{3}(1,2)==obj3

num(2)=1; end

if MFstate{6}(3,2)==obj6 && MFstate{1}(1,2)==obj1

num(3)=1; end

if MFstate{6}(2,1)==obj6 && MFstate{5}(1,2)==obj5

num(4)=1; end

%---十字拼完

if sum(num)==4; return;end

if num(1)==0

if MFstate{2}(1,2)==obj6 && MFstate{4}(3,2)==obj2

[MFstate,TnMn]=disorganize(MFstate,'dlBL');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(1,2)==obj2 && MFstate{4}(3,2)==obj6

[MFstate,TnMn]=disorganize(MFstate,'b2');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(2,3)==obj6 && MFstate{3}(2,3)==obj2

[MFstate,TnMn]=disorganize(MFstate,'rdRb2');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(2,3)==obj2 && MFstate{3}(2,3)==obj6

[MFstate,TnMn]=disorganize(MFstate,'b');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(2,1)==obj6 && MFstate{5}(2,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'LDlb2');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(2,1)==obj2 && MFstate{5}(2,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'B');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(3,2)==obj6 && MFstate{6}(1,2)==obj2

[MFstate,TnMn]=disorganize(MFstate,'bLDlb2');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(1,2)==obj6 && MFstate{6}(2,3)==obj2

[MFstate,TnMn]=disorganize(MFstate,'rb');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(1,2)==obj2 && MFstate{6}(2,3)==obj6

[MFstate,TnMn]=disorganize(MFstate,'r2db2Dr2');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(1,2)==obj6 && MFstate{6}(3,2)==obj2

[MFstate,TnMn]=disorganize(MFstate,'f2dRbr');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(1,2)==obj2 && MFstate{6}(3,2)==obj6

[MFstate,TnMn]=disorganize(MFstate,'f2d2b2');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,2)==obj6 && MFstate{6}(2,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'LB');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,2)==obj2 && MFstate{6}(2,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'L2Db2');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(3,2)==obj2 && MFstate{4}(2,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'Db2');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(3,2)==obj6 && MFstate{4}(2,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'lBL');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(2,3)==obj6 && MFstate{1}(2,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'l2BL2');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(2,3)==obj2 && MFstate{1}(2,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'lDLB2');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(3,2)==obj2 && MFstate{4}(1,2)==obj6

[MFstate,TnMn]=disorganize(MFstate,'D2B2');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(3,2)==obj6 && MFstate{4}(1,2)==obj2

[MFstate,TnMn]=disorganize(MFstate,'dRbr');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(2,3)==obj6 && MFstate{3}(2,1)==obj2

[MFstate,TnMn]=disorganize(MFstate,'RdrB2');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(2,3)==obj2 && MFstate{3}(2,1)==obj6

[MFstate,TnMn]=disorganize(MFstate,'fd2FB2');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(3,2)==obj2 && MFstate{4}(2,3)==obj6

[MFstate,TnMn]=disorganize(MFstate,'dB2');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(3,2)==obj6 && MFstate{4}(2,3)==obj2

[MFstate,TnMn]=disorganize(MFstate,'Rbr');TurnManu=[TurnManu;TnMn];

end

% 恢复完成

num(1)=1;

end

end

%% 将第二层4个角恢复

function [MFstate,TurnManu,num]=recover2F4(MFstate,num)

TurnManu=[];

%---第一层恢复

%记录面的位置

obj1=MFstate{1}(2,2);obj2=MFstate{2}(2,2);

obj3=MFstate{3}(2,2);obj4=MFstate{4}(2,2);

obj5=MFstate{5}(2,2);obj6=MFstate{6}(2,2);

if MFstate{1}(2,3)==obj1 && MFstate{3}(2,1)==obj3

num(1)=1; end

if MFstate{3}(2,3)==obj3 && MFstate{2}(2,3)==obj2

num(2)=1; end

if MFstate{2}(2,1)==obj2 && MFstate{5}(2,1)==obj5

num(3)=1; end

if MFstate{1}(2,1)==obj1 && MFstate{5}(2,3)==obj5

num(4)=1; end

%---第二层拼完

if sum(num)==4; return;end

if num(1)==0

if MFstate{1}(2,3)==obj3 && MFstate{3}(2,1)==obj1

[MFstate,TnMn]=disorganize(MFstate,'rURUFufUrURUFuf');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(2,3)==obj3 && MFstate{2}(2,3)==obj1

[MFstate,TnMn]=disorganize(MFstate,'ArURUFufarURUFuf');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(2,3)==obj1 && MFstate{2}(2,3)==obj3

[MFstate,TnMn]=disorganize(MFstate,'ArURUFufauFufurUR');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(2,1)==obj3 && MFstate{5}(2,1)==obj1

[MFstate,TnMn]=disorganize(MFstate,'a2rURUFufA2urURUFuf');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(2,1)==obj1 && MFstate{5}(2,1)==obj3

[MFstate,TnMn]=disorganize(MFstate,'a2rURUFufA2U2FufurUR');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(2,1)==obj1 && MFstate{5}(2,3)==obj3

[MFstate,TnMn]=disorganize(MFstate,'arURUFufAu2rURUFuf');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(2,1)==obj3 && MFstate{5}(2,3)==obj1

[MFstate,TnMn]=disorganize(MFstate,'arURUFufAUFufurUR');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(1,2)==obj3 && MFstate{6}(2,3)==obj1

[MFstate,TnMn]=disorganize(MFstate,'UFufurUR');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(3,2)==obj3 && MFstate{6}(1,2)==obj1

[MFstate,TnMn]=disorganize(MFstate,'FufurUR');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,2)==obj3 && MFstate{6}(2,1)==obj1

[MFstate,TnMn]=disorganize(MFstate,'uFufurUR');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(1,2)==obj3 && MFstate{6}(3,2)==obj1

[MFstate,TnMn]=disorganize(MFstate,'u2FufurUR');TurnManu=[TurnManu;TnMn];

elseif MFstate{3}(1,2)==obj1 && MFstate{6}(2,3)==obj3

[MFstate,TnMn]=disorganize(MFstate,'u2rURUFuf');TurnManu=[TurnManu;TnMn];

elseif MFstate{2}(3,2)==obj1 && MFstate{6}(1,2)==obj3

[MFstate,TnMn]=disorganize(MFstate,'UrURUFuf');TurnManu=[TurnManu;TnMn];

elseif MFstate{5}(1,2)==obj1 && MFstate{6}(2,1)==obj3

[MFstate,TnMn]=disorganize(MFstate,'rURUFuf');TurnManu=[TurnManu;TnMn];

elseif MFstate{1}(1,2)==obj1 && MFstate{6}(3,2)==obj3

[MFstate,TnMn]=disorganize(MFstate,'urURUFuf');TurnManu=[TurnManu;TnMn];

end

% 恢复完成

num(1)=1;

end

end

%% 公式恢复第三层的十字

function [MFstate,TurnManu]=recover3F10(MFstate)

TurnManu=[];

flg=0;

while flg~=1

flg=is10ok(MFstate);

if flg==1

break;

elseif flg==2

[MFstate,TnMu]=disorganize(MFstate,'u');TurnManu=[TurnManu;TnMu];

elseif flg==3

[MFstate,TnMu]=disorganize(MFstate,'U');TurnManu=[TurnManu;TnMu];

elseif flg==4

[MFstate,TnMu]=disorganize(MFstate,'U2');TurnManu=[TurnManu;TnMu];

elseif flg==5

[MFstate,TnMu]=disorganize(MFstate,'u');TurnManu=[TurnManu;TnMu];

end

[MFstate,TnMu]=disorganize(MFstate,'RUFufr');TurnManu=[TurnManu;TnMu];

end

end

function flg=is10ok(MFstate)

flg=0;

if MFstate{6}(2) == MFstate{6}(5) &&...

MFstate{6}(4) == MFstate{6}(5) &&...

MFstate{6}(6) == MFstate{6}(5) &&...

MFstate{6}(8) == MFstate{6}(5)

flg=1;

elseif MFstate{6}(4)==MFstate{6}(5) && MFstate{6}(6)==MFstate{6}(5)

flg=2;

elseif MFstate{6}(4)==MFstate{6}(5) && MFstate{6}(8)==MFstate{6}(5)

flg=3;

elseif MFstate{6}(6)==MFstate{6}(5) && MFstate{6}(8)==MFstate{6}(5)

flg=4;

elseif MFstate{6}(2)==MFstate{6}(5) && MFstate{6}(6)==MFstate{6}(5)

flg=5;

end

end

%% 将第三层4个角位置对应上

function [MFstate,TurnManu]=recover3F4(MFstate)

TurnManu=[];

flg=0;

while flg~=1

[flg,num]=is4ok(MFstate);

while sum(num)<2

[MFstate,TnMu]=disorganize(MFstate,'U');TurnManu=[TurnManu;TnMu];

[flg,num]=is4ok(MFstate);

end

if flg==1

break;

elseif flg==2

if num(4)==1

[MFstate,TnMu]=disorganize(MFstate,'a');TurnManu=[TurnManu;TnMu];

end

elseif flg==3

[MFstate,TnMu]=disorganize(MFstate,'A');TurnManu=[TurnManu;TnMu];

elseif flg==4

[MFstate,TnMu]=disorganize(MFstate,'A2');TurnManu=[TurnManu;TnMu];

elseif flg==5

[MFstate,TnMu]=disorganize(MFstate,'a');TurnManu=[TurnManu;TnMu];

end

[MFstate,TnMu]=disorganize(MFstate,'rULuRUlU2');TurnManu=[TurnManu;TnMu];

end

end

function [flg,num]=is4ok(MFstate)

num=zeros(1,4);

%记录面的位置

o1=MFstate{1}(2,2);o2=MFstate{2}(2,2);

o3=MFstate{3}(2,2);o4=MFstate{4}(2,2);

o5=MFstate{5}(2,2);o6=MFstate{6}(2,2);

A=[o1,o3,o6;o3,o2,o6;o2,o5,o6;o1,o5,o6];

B=[MFstate{1}(1,3),MFstate{3}(1,1),MFstate{6}(3,3);...

MFstate{3}(1,3),MFstate{2}(3,3),MFstate{6}(1,3);...

MFstate{2}(3,1),MFstate{5}(1,1),MFstate{6}(1,1);...

MFstate{5}(1,3),MFstate{1}(1,1),MFstate{6}(3,1);...

];

flg=0;

for n=1:4

num(n) = isfit(A(n,:),B(n,:));

end

if sum(num)==4

flg=1;

elseif num(1)==1

flg=2;

elseif num(2)==1

flg=3;

elseif num(3)==1

flg=4;

elseif num(4)==1

flg=5;

end

end

function flg=isfit(a,b)

flg=0;

if (a(1)==b(1) && a(2)==b(2) && a(3)==b(3)) ||...

(a(1)==b(2) && a(2)==b(3) && a(3)==b(1)) ||...

(a(1)==b(3) && a(2)==b(1) && a(3)==b(2)) ||...

(a(1)==b(1) && a(2)==b(3) && a(3)==b(2)) ||...

(a(1)==b(2) && a(2)==b(1) && a(3)==b(3)) ||...

(a(1)==b(3) && a(2)==b(2) && a(3)==b(1))

flg=1;

end

end

%% 顶面恢复

function [MFstate,TurnManu]=recover3Face(MFstate)

TurnManu=[];

num=0;

for n=1:3

for nn=1:3

if MFstate{6}(n,nn)==MFstate{6}(2,2)

num=num+1;

end

end

end

if num==9

return;%顶面恢复完成

elseif num==5

while (MFstate{2}(3,1)==MFstate{6}(2,2) || MFstate{2}(3,3)==MFstate{6}(2,2))

[MFstate,TnMu]=disorganize(MFstate,'a');TurnManu=[TurnManu;TnMu];

end

[MFstate,TnMu]=disorganize(MFstate,'RUrURU2rU2');TurnManu=[TurnManu;TnMu];

elseif num==6

while (MFstate{6}(1,1)~=MFstate{6}(2,2) )

[MFstate,TnMu]=disorganize(MFstate,'a');TurnManu=[TurnManu;TnMu];

end

[MFstate,TnMu]=disorganize(MFstate,'RUrURU2rU2');TurnManu=[TurnManu;TnMu];

elseif num==7

Tn=0;

while Tn<5 && ((MFstate{6}(1,3)~=MFstate{6}(2,2) || ...

MFstate{6}(2,3)~=MFstate{6}(2,2) || ...

MFstate{6}(3,3)~=MFstate{6}(2,2)))

[MFstate,TnMu]=disorganize(MFstate,'a');TurnManu=[TurnManu;TnMu];

Tn=Tn+1;

end

[MFstate,TnMu]=disorganize(MFstate,'RUrURU2rU2');TurnManu=[TurnManu;TnMu];

end

end

%% 顶棱中恢复

function [MFstate,TurnManu]=recover3FArris(MFstate)

TurnManu=[];

if MFstate{1}(1,2)==MFstate{1}(2,2) && MFstate{3}(1,2)==MFstate{3}(2,2)

return;

elseif MFstate{1}(1,2)==MFstate{1}(2,2)

[MFstate,TnMu]=disorganize(MFstate,'a2');TurnManu=[TurnManu;TnMu];

elseif MFstate{3}(1,2)==MFstate{3}(2,2)

[MFstate,TnMu]=disorganize(MFstate,'a');TurnManu=[TurnManu;TnMu];

elseif MFstate{5}(1,2)==MFstate{5}(2,2)

[MFstate,TnMu]=disorganize(MFstate,'A');TurnManu=[TurnManu;TnMu];

end

if MFstate{1}(1,2)==MFstate{3}(2,2)

[MFstate,TnMu]=disorganize(MFstate,'F2URlF2rLUF2');TurnManu=[TurnManu;TnMu];

else

[MFstate,TnMu]=disorganize(MFstate,'F2uRlF2rLuF2');TurnManu=[TurnManu;TnMu];

end

end

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/427799.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt 常用类——QStandardItemModel

转载&#xff1a;落叶知秋时 类QabstractItemModel&#xff0c;QabstractListModel&#xff0c;QAbstractTableModel不保存数据&#xff0c;用户需要从这些类派生出子类&#xff0c;并在子类中定义某种数据结构来保存数据。 与此不同&#xff0c;类QStandardItemModel负责保存数…

ad19原理图标注_AD19中原理图的模板如何进行编辑?

我们在进行原理图设计的时候&#xff0c;有时候不想去用软件自带默认的模板&#xff0c;想要用自己设计的模板&#xff0c;就涉及到我们的模板怎么去编辑的呢&#xff1f;我们应该如何去编辑原理图自己设计的模板&#xff1f;操作步骤是怎么的呢&#xff1f;我们今天就以AD19为…

mysql+秘密_mysql不被人知的秘密

http://blog.csdn.net/yueguanghaidao/article/details/69333872011先建立两张表。1.student表2.grade表一&#xff1a;mysql的复制技术1.表与数据的复制->>实现表结构和数据的同步create table desttable select * from srctable;(desttable:目标表&#xff0c;srctable…

mysql一个表几亿数据_如何在mysql 造1亿条记录的大容量数据表?

背景及目标&#xff1a;现有数据1000w单表&#xff0c;为压力测试准备1亿条数据。步骤&#xff1a;1.将1000w条记录&#xff0c;除id外都导入到多个文件中&#xff1a;//DELIMITERDROP PROCEDURE if EXISTS createManyTable;create PROCEDURE createManyTable()BEGINDECLARE i …

windows键盘在mac上怎么识别_Mac电脑怎么使用pc键盘?

苹果电脑一般需要使用配套的mac键盘&#xff0c;毕竟按键和普通pc键盘不一样。但是总会遇到一些情况&#xff0c;比如配套的mac键盘坏了&#xff0c;而刚好有一个pc键盘&#xff0c;那么这时候苹果电脑要怎么用pc键盘呢&#xff1f;这边小编跟大家介绍mac电脑连接pc键盘以及设置…

LOJ#6002. 「网络流 24 题」最小路径覆盖

模板。 1 #include<iostream>2 #include<cstring>3 #include<cstdio>4 //#include<time.h>5 //#include<complex>6 //#include<set>7 //#include<queue>8 #include<algorithm>9 #include<stdlib.h>10 using namespace s…

zabbix mysql设置中文乱码_解决zabbix监控因php问题导致图形界面中文乱码方法

解决因编译php中添加了-enable-gd-jis-conv选项导致Zabbix监控系统图形界面中文乱码问题现象&#xff1a;php编译参数&#xff1a;说明&#xff1a;如果PHP编译时启用–enable-gd-jis-conv选项的话&#xff0c;那么非ASCII字符(例如汉字、拼音、希腊文和箭头) 会被当成EUC-JP编…

熟悉常用的HDFS操作

一、Hadoop提供的Shell命令完成相同任务&#xff1a; 在本地Linux文件系统的“/home/hadoop/”目录下创建一个文件txt&#xff0c;里面可以随意输入一些单词.在本地查看文件位置&#xff08;ls&#xff09;在本地显示文件内容使用命令把本地文件系统中的“txt”上传到HDFS中的当…

mysql 低端_mysql入门

相关了解(重点内容从标题一开始)目前主流数据库有:sqlserver, mysql, Qracle, SQLite, Access, MS SQL Server等, 我主要说的是mysql;以下几点需要记住!!!SQL包括了所有对数据库的操作, 主要由4个部分组1. 数据库定义语言(DDL): 用于定义和管理数据库对象, 包括数据库, 基本表,…

阿里云服务器由于被检测到对外攻击,已阻断该服务器对其它服务器端口的访问...

问题&#xff1a; (系统&#xff1a;CentOS 7.2 ) 前几天在阿里云服务器安装了Redis, 刚开始没有设置redis密码, 后台阿里云发送多次邮件提示如下&#xff1a; 经过几次波折才发现并彻底解决了问题&#xff1a; 原因&#xff1a; 估计是因为 Redis服务开启时没有设置密码 &…

批量找注入 python3+sqlmap结合

注入一直都是用sqlmap 导致本来就不怎么精通的手工注入现在就忘的一干二净 想实战练习 却一时又找不到有注入的网站 于是便有了这篇文章 想找个批量获取域名链接的工具 但都是只是获取域名而已 都没获取后面的参数 于是自己写了个只获取bing前10页的结果 输入q 结束循环…

golang 反射

参考&#xff1a;|--http://blog.51cto.com/speakingbaicai/1707637 |--https://studygolang.com/articles/6324 反射是在golang程序运行时检查变量所具有类型的一种机制。由于反射可以得出关于变量结构的数据&#xff08;即“关于数据的数据”&#xff09;&#xff0c;所以这也…

java硬件编程_关于JAVA并发编程你需要知道的——硬件篇

无论程序语言如何千变万化&#xff0c;他们都深深地根植于目前的计算机体系结构。左图是intel CPU的三级高速缓存设计&#xff0c;由于高速缓存对程序员基本不可见&#xff0c;因此可以抽象为右图。缓存的设计首先还是先谈谈左图。L1-cache分为两部分&#xff0c;i-cache存储指…

net Core做一个webApi的简单实例

用NetCore 和Dapper 和mySql做一个简单的实例&#xff0c; 一准备工作 1&#xff1a;VS2017windos系统&#xff0c;也可以用其他的操作系统和工具 2&#xff1a;一台Cenetos的虚拟机或者虚拟机 二&#xff1a;开始 1&#xff1a;用微软官方的netCore的ToDo项目改造&#xff0c;…

java 文件输出流_Java 文件输出流

Java IO教程 - Java文件输出流创建输出流要写入文件&#xff0c;我们需要创建一个FileOutputStream类的对象&#xff0c;它将表示输出流。// Create a file output streamString destFile "test.txt";FileOutputStream fos new FileOutputStream(destFile);当写入文…

MySQL5.7参数log_timestamps

最近测试MySQL 5.7.21 Community Server这个版本的MySQL数据库时&#xff0c;发现其错误日志的时间跟系统当前时间不一致&#xff0c;后面检查发现日期时间格式都是UTC时间&#xff0c;查了一下相关资料&#xff0c;原来在MySQL 5.7.2 之后日志文件里面的时间戳从默认的本地系…

Tidb集群加mysql_TiDB - 快速入门,集群搭建

TiDB 是开源分布式关系型数据库&#xff0c;是一款同时支持在线事务处理与在线分析处理 (Hybrid Transactional and Analytical Processing, HTAP)的融合型分布式数据库产品&#xff0c;具备水平扩容或者缩容、金融级高可用、实时 HTAP、云原生的分布式数据库、兼容 MySQL 5.7 …

python递归函数

递归函数简单来说就是函数的自我调用。使用递归函数很多时候可以使得代码简洁&#xff0c;优雅。可以把复杂的问题分解成简单的子问题。递归有无与伦比的魅力&#xff0c;从著名的计算机名言就可以看出递归的奇妙&#xff1a; To iterate is human,to recurse divine. 迭代者为…

java知识体系 servlet_03-Servlet 体系结构知识梳理

一、Servlet体系结构Java Web应用是基于Servlet规范运行&#xff0c;Servlet顶层类的关联如下图&#xff1a;从图可看出&#xff0c;Servlet规范基本围绕这几个类运行&#xff0c;其中&#xff0c;与Servlet主动关联的有3个类&#xff0c;分别是ServletRequest、ServletRespons…

testlink自带java api_java如何连接testlink

1.下载相关的jar包2.获取到testlink的url和key&#xff0c;注意&#xff1a;url不是testlink的连接地址&#xff0c;是连接地址/lib/api/xmlrpc.php3.测试是否连接成功public static void main(String args[]) {String url "http://test.tl.gmsd.lan/lib/api/xmlrpc.php&…