📄 bsmdlg.cpp
字号:
GetDlgItem(IDC_EDIT_STATION)->SetWindowText(m_str2+m_str3);
}
else
GetDlgItem(IDC_EDIT_STATION)->SetWindowText("Fail to find!");
return TRUE;
}
void CBSMDlg::OnStartend()
{
// TODO: Add your control notification handler code here
UpdateData();
step=0;
m_byBoard[0]=m_ori1;
m_byBoard[1]=m_ori2;
m_byBoard[2]=m_ori3;
m_byBoard[3]=m_ori4;
m_byBoard[4]=m_ori5;
m_byBoard[5]=m_ori6;
m_byBoard[6]=m_ori7;
m_byBoard[7]=m_ori8;
m_byBoard[8]=m_ori9;
m_target[0]=atoi(m_end1);
m_target[1]=atoi(m_end2);
m_target[2]=atoi(m_end3);
m_target[3]=atoi(m_end4);
m_target[4]=atoi(m_end5);
m_target[5]=atoi(m_end6);
m_target[6]=atoi(m_end7);
m_target[7]=atoi(m_end8);
m_target[8]=atoi(m_end9);
// m_str1="the initial numbers are :\r\n"+m_ori1+" "+m_ori2+" "+m_ori3+"\r\n"+m_ori4+" "+m_ori5+" "+m_ori6+"\r\n"+m_ori7+" "+m_ori8+" "+m_ori9;
m_str1.Format("the initial numbers are:\r\n%d %d %d\r\n%d %d %d\r\n%d %d %d",m_ori1,m_ori2,m_ori3,m_ori4,m_ori5,m_ori6,m_ori7,m_ori8,m_ori9);
m_str2="the object numbers are :\r\n"+m_end1+" "+m_end2+" "+m_end3+"\r\n"+m_end4+" "+m_end5+" "+m_end6+"\r\n"+m_end7+" "+m_end8+" "+m_end9;
GetDlgItem(IDC_EDIT_STATION)->SetWindowText(m_str1+"\r\n"+m_str2);
}
int CBSMDlg::move_up(int num[])
{
for (int i=0;i<9;i++)
if (num[i]==0)
break;
if (i<3)
return 0;
else
{
num[i]=num[i-3];
num[i-3]=0;
return 1;
}
}
int CBSMDlg::move_down(int num[])
{
for (int i=0;i<9;i++)
if (num[i]==0)
break;
if (i>5)
return 0;
else
{
num[i]=num[i+3];
num[i+3]=0;
return 1;
}
}
int CBSMDlg::move_left(int num[])
{
for (int i=0;i<9;i++)
if (num[i]==0)
break;
if (i==0||i==3||i==6)
return 0;
else
{
num[i]=num[i-1];
num[i-1]=0;
return 1;
}
}
int CBSMDlg::move_right(int num[])
{
for (int i=0;i<9;i++)
if (num[i]==0)
break;
if (i==2||i==5||i==8)
return 0;
else
{
num[i]=num[i+1];
num[i+1]=0;
return 1;
}
}
int CBSMDlg::icansolve(int num[], int target[])
{
int i,j;
int count_num,count_target;
for (i=0;i<9;i++)
for (j=0;j<i;j++)
{
if(num[j]<num[i]&&num[j]!=0)
count_num++;
if(target[j]<target[i]&&target[j]!=0)
count_target++;
}
count_num=count_num-2*(count_num/2);
count_target=count_target-2*(count_target/2);
if ((count_num==1&&count_target==1)||(count_num==0&&count_target==0))
return 1;
else
return 0;
}
int CBSMDlg::existed(int num[], EightNum *where)
{
EightNum *p;
for(p=where;p!=NULL;p=p->parent)
if(*p==num)
return 1;
return 0;
}
EightNum* CBSMDlg::find_OK_leaf(EightNum *start)
{
EightNum *p,*OK;
p=OK=start;
int min=start->get_evafun();
for(p=start;p!=NULL;p=p->leaf_next)
if(min>p->get_evafun())
{
OK=p;
min=p->get_evafun();
}
return OK;
}
BOOL CBSMDlg::OnGuangdu()
{
// TODO: Add your control notification handler code here
EightNum S(m_byBoard),Target(m_target);
S.parent=S.open_next=NULL;
if(!icansolve(m_byBoard,m_target))
{
GetDlgItem(IDC_EDIT_STATION)->SetWindowText("Fail!No one can do it !");
return FALSE;
}
clock_t Start,Finish;
double duration;
Start=clock( );
int flag=0;
EightNum *open_head=&S,*open_tos=&S,*new_8num;
while(open_head!=NULL&&flag!=1)
{
if(*open_head==Target)
{
flag=1;
break;
}
open_head->get_numbers_to(m_byBoard);
if(move_up(m_byBoard)&&!existed(m_byBoard,open_head))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_head;
new_8num->open_next=NULL;
open_tos->open_next=new_8num;
open_tos=new_8num;
}
open_head->get_numbers_to(m_byBoard);
if(move_down(m_byBoard)&&!existed(m_byBoard,open_head))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_head;
new_8num->open_next=NULL;
open_tos->open_next=new_8num;
open_tos=new_8num;
}
open_head->get_numbers_to(m_byBoard);
if(move_left(m_byBoard)&&!existed(m_byBoard,open_head))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_head;
new_8num->open_next=NULL;
open_tos->open_next=new_8num;
open_tos=new_8num;
}
open_head->get_numbers_to(m_byBoard);
if(move_right(m_byBoard)&&!existed(m_byBoard,open_head))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_head;
new_8num->open_next=NULL;
open_tos->open_next=new_8num;
open_tos=new_8num;
}
open_head=open_head->open_next;
}
Finish=clock( );
duration=Finish-Start;
if(flag==1)
{
EightNum *p;
for (p=open_head->parent;p!=NULL;p=p->parent)
{
step++;
p->get_numbers_to(m_byBoard);
m_ori1=m_byBoard[0];
m_ori2=m_byBoard[1];
m_ori3=m_byBoard[2];
m_ori4=m_byBoard[3];
m_ori5=m_byBoard[4];
m_ori6=m_byBoard[5];
m_ori7=m_byBoard[6];
m_ori8=m_byBoard[7];
m_ori9=m_byBoard[8];
m_str1.Format("\r\nthe %d step is:\r\n%d %d %d\r\n%d %d %d\r\n%d %d %d",step,m_ori1,m_ori2,m_ori3,m_ori4,m_ori5,m_ori6,m_ori7,m_ori8,m_ori9);
m_str2+=m_str1;
GetDlgItem(IDC_EDIT_STATION)->SetWindowText(m_str2);
}
m_str3.Format("\r\nCongratulation!the time is %f ms",duration);//"\r\nthe last step is:\r\n"+m_end1+" "+m_end2+" "+m_end3+"\r\n"+m_end4+" "+m_end5+" "+m_end6+"\r\n"+m_end7+" "+m_end8+" "+m_end9;
GetDlgItem(IDC_EDIT_STATION)->SetWindowText(m_str2+m_str3);
}
else
GetDlgItem(IDC_EDIT_STATION)->SetWindowText("Fail to find!");
return 0;
}
BOOL CBSMDlg::OnShendu()
{
// TODO: Add your control notification handler code here
int max_step=10;
double duration;
clock_t Start,Finish;
int flag=0;//是否查找成功标志,1表示成功
int new_flag=0;//是否有新节点生成,1表示有
Start=clock();
EightNum S(m_byBoard),Target(m_target);
S.parent=S.open_pre=NULL;
S.cul_para();
EightNum *open_tos=&S,*new_8num,*p;
while(open_tos!=NULL&&flag!=1)
{
if(*open_tos==Target)
{
flag=1;
break;
}
new_flag=0;
p=open_tos->open_pre;
if(open_tos->get_deapth()>=max_step)
{
open_tos=open_tos->open_pre;
continue;
}
open_tos->get_numbers_to(m_byBoard);
if(move_up(m_byBoard)&&!existed(m_byBoard,open_tos))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_tos;
new_8num->open_pre=p;
open_tos->open_pre=NULL;
new_8num->cul_para();
new_flag=1;
p=new_8num;
}
open_tos->get_numbers_to(m_byBoard);
if(move_down(m_byBoard)&&!existed(m_byBoard,open_tos))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_tos;
new_8num->open_pre=p;
open_tos->open_pre=NULL;
new_8num->cul_para();
new_flag=1;
p=new_8num;
}
open_tos->get_numbers_to(m_byBoard);
if(move_left(m_byBoard)&&!existed(m_byBoard,open_tos))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_tos;
new_8num->open_pre=p;
open_tos->open_pre=NULL;
new_8num->cul_para();
new_flag=1;
p=new_8num;
}
open_tos->get_numbers_to(m_byBoard);
if(move_right(m_byBoard)&&!existed(m_byBoard,open_tos))
{
new_8num=new EightNum;
new_8num->set_num(m_byBoard);
new_8num->parent=open_tos;
new_8num->open_pre=p;
open_tos->open_pre=NULL;
new_8num->cul_para();
new_flag=1;
p=new_8num;
}
if(new_flag=1)
open_tos=new_8num;
else
open_tos=open_tos->open_pre;
}
Finish=clock( );
duration=Finish-Start;
if(flag==1)
{
EightNum *p;
for (p=open_tos->parent;p!=NULL;p=p->parent)
{
step++;
p->get_numbers_to(m_byBoard);
m_ori1=m_byBoard[0];
m_ori2=m_byBoard[1];
m_ori3=m_byBoard[2];
m_ori4=m_byBoard[3];
m_ori5=m_byBoard[4];
m_ori6=m_byBoard[5];
m_ori7=m_byBoard[6];
m_ori8=m_byBoard[7];
m_ori9=m_byBoard[8];
m_str1.Format("\r\nthe %d step is:\r\n%d %d %d\r\n%d %d %d\r\n%d %d %d",step,m_ori1,m_ori2,m_ori3,m_ori4,m_ori5,m_ori6,m_ori7,m_ori8,m_ori9);
m_str2+=m_str1;
GetDlgItem(IDC_EDIT_STATION)->SetWindowText(m_str2);
}
m_str3.Format("\r\nCongratulation!the time is %f ms",duration);//"\r\nthe last step is:\r\n"+m_end1+" "+m_end2+" "+m_end3+"\r\n"+m_end4+" "+m_end5+" "+m_end6+"\r\n"+m_end7+" "+m_end8+" "+m_end9;
GetDlgItem(IDC_EDIT_STATION)->SetWindowText(m_str2+m_str3);
}
else
GetDlgItem(IDC_EDIT_STATION)->SetWindowText("Fail to find!");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -