三阶魔方自动求解及动态可视化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开始?有人实现了吗?
参考链接
没作记录,忘了,对参考过代码的博主表示歉意,如有发现雷同的地方,欢迎提醒,以作补充。
源代码
%% 主调函数
% ---定义魔方的初始状态
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