📄
字号:
#include <iostream>
#include <string>
using namespace std;
char t[10];
int strdif(char s[]);
int strindex(char s[]);
inline void change(char &a,char &b);
class node
{
private:
char s[10];
char op;
int dif,index;
node *pre;
node *next;
node *openroot;
node *closeroot;
public:
node():op('\0'),dif(10),index(10),next(NULL),pre(NULL),openroot(NULL),closeroot(NULL)
{
strcpy(s,"\0");
}
void init(char s[])
{
node *temp;
int i=0;
openroot=new node;
closeroot=new node;
temp=new node;
openroot->next=temp;
strcpy(temp->s,s);
i=strdif(s);
temp->dif=i;
i=strindex(s);
temp->index=i;
}
void deal();
void develop(node *temp);
void prin(node *temp);
void move(node *temp,int s,int t,char op);
void destorynode()
{
node *temp;
while(openroot!=NULL)
{
temp=openroot->next;
delete openroot;
openroot=temp;
}
while(closeroot!=NULL)
{
temp=closeroot->next;
delete closeroot;
closeroot=temp;
}
cout<<"DESTORIED!"<<endl;
}
};
void node::deal()
{
node *temp,*cursor,*p,*q;
int i;
temp=cursor=p=q=NULL;
if(openroot->next==NULL)
{
cout<<"ERROR!"<<endl;
exit(1);
}
for(cursor=openroot,i=10;cursor->next!=NULL;cursor=cursor->next)
{
if((cursor->next->dif)<i)
{
i=cursor->next->dif;
q=cursor;
p=cursor->next;
}
}
q->next=p->next;
p->next=openroot->next;
openroot->next=p;
temp=openroot->next;
openroot->next=temp->next;
temp->next=closeroot->next;
closeroot->next=temp;
if(temp->dif==0)
{
prin(closeroot->next);
return;
}
develop(temp);
}
void node::develop(node *temp)
{
switch(temp->index)
{
case 0: move(temp,0,1,'R');
move(temp,0,3,'D');
break;
case 1: move(temp,1,0,'L');
move(temp,1,2,'R');
move(temp,1,4,'D');
break;
case 2: move(temp,2,5,'D');
move(temp,2,1,'L');
break;
case 3: move(temp,3,0,'U');
move(temp,3,4,'R');
move(temp,3,6,'D');
break;
case 4: move(temp,4,1,'U');
move(temp,4,5,'R');
move(temp,4,3,'L');
move(temp,4,7,'D');
break;
case 5: move(temp,5,2,'U');
move(temp,5,4,'L');
move(temp,5,8,'D');
break;
case 6: move(temp,6,3,'U');
move(temp,6,7,'R');
break;
case 7: move(temp,7,4,'U');
move(temp,7,8,'R');
move(temp,7,6,'L');
break;
case 8: move(temp,8,5,'U');
move(temp,8,7,'L');
break;
}
deal();
}
void node::move(node *temp,int s,int t,char op)
{
node *cursor,*p,*q;
char m[10];
strcpy(m,temp->s);
change(m[s],m[t]);
for(p=openroot->next,q=closeroot->next;p!=NULL||q!=NULL;)
{
if(p!=NULL)
{
if(strcmp(m,p->s)==0)
return;
p=p->next;
}
if(q!=NULL)
{
if(strcmp(m,q->s)==0)
return;
q=q->next;
}
}
cursor=new node;
strcpy(cursor->s,m);
cursor->op=op;
cursor->index=t;
cursor->dif=strdif(cursor->s);
cursor->pre=temp;
cursor->next=openroot->next;
openroot->next=cursor;
}
void node::prin(node *temp)
{
while(temp!=NULL)
{
cout<<temp->s<<" "<<temp->op<<endl;
temp=temp->pre;
}
}
void main()
{
node op;
char s[10];
cout<<"请输入你的初始状态:";
cin>>s;
s[9]='\0';
cin.clear();
cout<<"请输入你的目标状态:";
cin>>t;
t[9]='\0';
cin.clear();
op.init(s);
op.deal();
op.destorynode();
}
int strdif(char s[])
{
int i,j;
i=j=0;
for(;s[i]&&i<9;i++)
if(s[i]!=t[i]&&t[i]!='0')
j++;
return j;
}
int strindex(char s[])
{
int i=0;
for(;s[i]&&i<9;i++)
if(s[i]=='0')
break;
return i;
}
inline void change(char &a,char &b)
{
char c;
c=a;a=b;b=c;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -