📄 eightnumsearchdlg.cpp
字号:
// EightNumSearchDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EightNumSearch.h"
#include "EightNumSearchDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEightNumSearchDlg dialog
CEightNumSearchDlg::CEightNumSearchDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEightNumSearchDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEightNumSearchDlg)
m_Com1Str1 = _T("2");
m_Com1Str2 = _T("8");
m_Com1Str3 = _T("3");
m_Com1Str4 = _T("1");
m_Com1Str5 = _T("");
m_Com1Str6 = _T("4");
m_Com1Str7 = _T("7");
m_Com1Str8 = _T("6");
m_Com1Str9 = _T("5");
m_Com2Str1 = _T("1");
m_Com2Str2 = _T("2");
m_Com2Str3 = _T("3");
m_Com2Str4 = _T("8");
m_Com2Str5 = _T("");
m_Com2Str6 = _T("4");
m_Com2Str7 = _T("7");
m_Com2Str8 = _T("6");
m_Com2Str9 = _T("5");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEightNumSearchDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEightNumSearchDlg)
DDX_Control(pDX, IDC_NEXTSTEP, m_NextStep);
DDX_Control(pDX, IDC_FORTHSTEP, m_ForthStep);
DDX_Text(pDX, IDC_COM1_EDIT1, m_Com1Str1);
DDX_Text(pDX, IDC_COM1_EDIT2, m_Com1Str2);
DDX_Text(pDX, IDC_COM1_EDIT3, m_Com1Str3);
DDX_Text(pDX, IDC_COM1_EDIT4, m_Com1Str4);
DDX_Text(pDX, IDC_COM1_EDIT5, m_Com1Str5);
DDX_Text(pDX, IDC_COM1_EDIT6, m_Com1Str6);
DDX_Text(pDX, IDC_COM1_EDIT7, m_Com1Str7);
DDX_Text(pDX, IDC_COM1_EDIT8, m_Com1Str8);
DDX_Text(pDX, IDC_COM1_EDIT9, m_Com1Str9);
DDX_Text(pDX, IDC_COM2_EDIT1, m_Com2Str1);
DDX_Text(pDX, IDC_COM2_EDIT2, m_Com2Str2);
DDX_Text(pDX, IDC_COM2_EDIT3, m_Com2Str3);
DDX_Text(pDX, IDC_COM2_EDIT4, m_Com2Str4);
DDX_Text(pDX, IDC_COM2_EDIT5, m_Com2Str5);
DDX_Text(pDX, IDC_COM2_EDIT6, m_Com2Str6);
DDX_Text(pDX, IDC_COM2_EDIT7, m_Com2Str7);
DDX_Text(pDX, IDC_COM2_EDIT8, m_Com2Str8);
DDX_Text(pDX, IDC_COM2_EDIT9, m_Com2Str9);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEightNumSearchDlg, CDialog)
//{{AFX_MSG_MAP(CEightNumSearchDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SOLUTE, OnSolute)
ON_BN_CLICKED(IDC_NEXTSTEP, OnNextstep)
ON_BN_CLICKED(IDC_FORTHSTEP, OnForthstep)
ON_BN_CLICKED(IDC_INPUTORAL, OnInputoral)
ON_BN_CLICKED(IDC_INPUTOBJ, OnInputobj)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEightNumSearchDlg message handlers
BOOL CEightNumSearchDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
flat=false;
return TRUE; // return TRUE unless you set the focus to a control
}
void CEightNumSearchDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEightNumSearchDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEightNumSearchDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEightNumSearchDlg::match(int n)//判断是否为目标并且返回W值
{
int i,j;
int k=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
if(base[n].matrix[i][j]!=end[i][j])
{
k++;
}
}
base[n].w=k;
if(k==0)flat=true;
}
void CEightNumSearchDlg::findkg(int n)//判断空格的位置并返回该位置
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
if(base[n].matrix[i][j]==' ')
{
base[n].x=i;base[n].y=j;
}
}
}
void CEightNumSearchDlg::getf(int n)//获得估价函数的值
{
base[n].f=base[n].w+base[n].d;
}
void CEightNumSearchDlg::paixu(int n,int m)//按估价函数f排序,从m到n
{
int i,j,p;
struct Node q;
for(i=m;i<n;i++)
{
p=i;
/////////冒泡法,从小到大排列
for(j=i+1;j<n;j++)
if(base[p].f>=base[j].f)//找i后面的最小值
p=j;
////////////////把base[i]与base[p]进行交换
if(p==i)continue;
q=base[i];
base[i]=base[p];
base[p]=q;
}
}
void CEightNumSearchDlg::kong(int &m,int x,int y,int &n)//空格移动的控制
{
int i,j,z,k,o=n;
z=3*x+y;
int h=1;
struct Node q;
switch (z)
{
char a;
case 0: //空格位于[0][0],有两种移动,并获得移动后空格的位置及估价函数f值
base[n+1]=base[m];
a=base[n+1].matrix[0][0];
base[n+1].matrix[0][0]=base[n+1].matrix[0][1];
base[n+1].matrix[0][1]=a;
base[n+1].x=0;
base[n+1].y=1;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);//判断是否为目标并且返回W值
getf(n);
base[n+1]=base[m];
a=base[n+1].matrix[0][0];
base[n+1].matrix[0][0]=base[n+1].matrix[1][0];
base[n+1].matrix[1][0]=a;
base[n+1].x=1;
base[n+1].y=0;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
break;
case 1://空格位于[0][1],有三种移动,并获得移动后空格的位置及估价函数f值
base[n+1]=base[m];
a=base[n+1].matrix[0][1];
base[n+1].matrix[0][1]=base[n+1].matrix[0][0];
base[n+1].matrix[0][0]=a;
base[n+1].x=0;
base[n+1].y=0;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
base[n+1]=base[m];
a=base[n+1].matrix[0][1];
base[n+1].matrix[0][1]=base[n+1].matrix[0][2];
base[n+1].matrix[0][2]=a;
base[n+1].x=0;
base[n+1].y=2;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
base[n+1]=base[m];
a=base[n+1].matrix[0][1];
base[n+1].matrix[0][1]=base[n+1].matrix[1][1];
base[n+1].matrix[1][1]=a;
base[n+1].x=1;
base[n+1].y=1;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
break;
case 2://空格位于[0][2],有两种移动,并获得移动后空格的位置及估价函数f值
base[n+1]=base[m];
a=base[n+1].matrix[0][2];
base[n+1].matrix[0][2]=base[n+1].matrix[0][1];
base[n+1].matrix[0][1]=a;
base[n+1].x=0;
base[n+1].y=1;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
base[n+1]=base[m];
a=base[n+1].matrix[0][2];
base[n+1].matrix[0][2]=base[n+1].matrix[1][2];
base[n+1].matrix[1][2]=a;
base[n+1].x=1;
base[n+1].y=2;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
break;
case 3://空格位于[1][0],有三种移动,并获得移动后空格的位置及估价函数f值
base[n+1]=base[m];
a=base[n+1].matrix[1][0];
base[n+1].matrix[1][0]=base[n+1].matrix[0][0];
base[n+1].matrix[0][0]=a;
base[n+1].x=0;
base[n+1].y=0;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
base[n+1]=base[m];
a=base[n+1].matrix[1][0];
base[n+1].matrix[1][0]=base[n+1].matrix[1][1];
base[n+1].matrix[1][1]=a;
base[n+1].x=1;
base[n+1].y=1;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
base[n+1]=base[m];
a=base[n+1].matrix[1][0];
base[n+1].matrix[1][0]=base[n+1].matrix[2][0];
base[n+1].matrix[2][0]=a;
base[n+1].x=2;
base[n+1].y=0;
base[n+1].d=base[m].d+1;
n=n+1;
match(n);
getf(n);
break;
case 4://空格位于[1][1],有四种移动,并获得移动后空格的位置及估价函数f值
base[n+1]=base[m];
a=base[n+1].matrix[1][1];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -