⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aiproject1dlg.cpp

📁 人工智能搜索法解决九宫重排问题(又称8数码问题)。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AIProject1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "AIProject1.h"
#include "AIProject1Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CAIProject1Dlg dialog

CAIProject1Dlg::CAIProject1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAIProject1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAIProject1Dlg)
	m_strEdit = _T("");
	m_nNum1 = 8;
	m_nNum2 = 1;
	m_nNum3 = 2;
	m_nNum4 = 6;
	m_nNum5 = 0;
	m_nNum6 = 3;
	m_nNum7 = 7;
	m_nNum8 = 5;
	m_nNum9 = 4;
	m_nCostChose = -1;
	m_nTimes = 0;
	m_nDepth = 0;
	
	m_nNum11 = 1;
	m_nNum12 = 2;
	m_nNum13 = 3;
	m_nNum14 = 8;
	m_nNum15 = 0;
	m_nNum16 = 4;
	m_nNum17 = 7;
	m_nNum18 = 6;
	m_nNum19 = 5;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_nDepth=0;
	m_nTimes=0;
	m_nCostChose=0;
	depthCount=0;
	start[0]=100;
	start[10]=start[11]=0;
	for(int i=1;i<10;i++){
		start[i]=0;end[i]=0;
	}
	
}

void CAIProject1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAIProject1Dlg)
	DDX_Text(pDX, IDC_EDIT10, m_strEdit);
	DDX_Text(pDX, IDC_EDIT1, m_nNum1);
	DDV_MinMaxUInt(pDX, m_nNum1, 0, 8);
	DDX_Text(pDX, IDC_EDIT2, m_nNum2);
	DDV_MinMaxUInt(pDX, m_nNum2, 0, 8);
	DDX_Text(pDX, IDC_EDIT3, m_nNum3);
	DDV_MinMaxUInt(pDX, m_nNum3, 0, 8);
	DDX_Text(pDX, IDC_EDIT4, m_nNum4);
	DDV_MinMaxUInt(pDX, m_nNum4, 0, 8);
	DDX_Text(pDX, IDC_EDIT5, m_nNum5);
	DDV_MinMaxUInt(pDX, m_nNum5, 0, 8);
	DDX_Text(pDX, IDC_EDIT6, m_nNum6);
	DDV_MinMaxUInt(pDX, m_nNum6, 0, 8);
	DDX_Text(pDX, IDC_EDIT7, m_nNum7);
	DDV_MinMaxUInt(pDX, m_nNum7, 0, 8);
	DDX_Text(pDX, IDC_EDIT8, m_nNum8);
	DDV_MinMaxUInt(pDX, m_nNum8, 0, 8);
	DDX_Text(pDX, IDC_EDIT9, m_nNum9);
	DDV_MinMaxUInt(pDX, m_nNum9, 0, 8);
	DDX_Radio(pDX, IDC_COST1, m_nCostChose);
	DDX_Text(pDX, IDC_TIMES, m_nTimes);
	DDX_Text(pDX, IDC_DEPTH, m_nDepth);
	DDX_Text(pDX, IDC_EDIT11, m_nNum11);
	DDV_MinMaxUInt(pDX, m_nNum11, 0, 8);
	DDX_Text(pDX, IDC_EDIT12, m_nNum12);
	DDV_MinMaxUInt(pDX, m_nNum12, 0, 8);
	DDX_Text(pDX, IDC_EDIT13, m_nNum13);
	DDV_MinMaxUInt(pDX, m_nNum13, 0, 8);
	DDX_Text(pDX, IDC_EDIT14, m_nNum14);
	DDV_MinMaxUInt(pDX, m_nNum14, 0, 8);
	DDX_Text(pDX, IDC_EDIT15, m_nNum15);
	DDV_MinMaxUInt(pDX, m_nNum15, 0, 8);
	DDX_Text(pDX, IDC_EDIT16, m_nNum16);
	DDV_MinMaxUInt(pDX, m_nNum16, 0, 8);
	DDX_Text(pDX, IDC_EDIT17, m_nNum17);
	DDV_MinMaxUInt(pDX, m_nNum17, 0, 8);
	DDX_Text(pDX, IDC_EDIT18, m_nNum18);
	DDV_MinMaxUInt(pDX, m_nNum18, 0, 8);
	DDX_Text(pDX, IDC_EDIT19, m_nNum19);
	DDV_MinMaxUInt(pDX, m_nNum19, 0, 8);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAIProject1Dlg, CDialog)
	//{{AFX_MSG_MAP(CAIProject1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BEGIN, OnBegin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAIProject1Dlg message handlers

BOOL CAIProject1Dlg::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

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAIProject1Dlg::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 CAIProject1Dlg::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 CAIProject1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAIProject1Dlg::OnBegin() 
{
	// TODO: Add your control notification handler code here
	const int times=200;
	int i,j;
	BOOL bUpdateData=UpdateData(TRUE);		
	m_strEdit.Format("");
	start[1]=m_nNum1;
	start[2]=m_nNum2;
	start[3]=m_nNum3;
	start[4]=m_nNum4;
	start[5]=m_nNum5;
	start[6]=m_nNum6;
	start[7]=m_nNum7;
	start[8]=m_nNum8;
	start[9]=m_nNum9;
	end[0]=100;
	end[1]=m_nNum11;end[2]=m_nNum12;end[3]=m_nNum13;end[4]=m_nNum14;
	end[5]=m_nNum15;end[6]=m_nNum16;end[7]=m_nNum17;end[8]=m_nNum18;end[9]=m_nNum19;
	
	for(i=1;i<9;i++)
		for(j=i+1;j<10;j++){
			if((start[i]-start[j])==0)
				 bUpdateData=0;
		}
	for(i=1;i<9;i++)
		for(j=i+1;j<10;j++){
			if((end[i]-end[j])==0)
				 bUpdateData=0;
			}

	if(!bUpdateData){
		MessageBox("The data you put in is incorrect!");		
		return;
	}
	
	for(i=1;i<10;i++)
	{
		if(start[i]!=end[i])
			break;
	}

	if(i==10) 
	{	
		MessageBox("The state you put in is the result!");
		UpdateData(FALSE);
		return;
	}
	else 
	{
		start[10]=0;	
		start[11]=00;
		if(m_nCostChose==0)
			start[0]=CostFunction1(start);
		if(m_nCostChose==1)
			start[0]=CostFunction2(start);
		if(m_nCostChose==2)
			start[0]=CostFunction3(start);
		for(i=0;i<12;i++)
		{
			nodeForExp[1][i]=start[i];
		//	nodePath[1][i]=start[i];
		}
		m_strEdit.Format(m_strEdit+"The start state:\r\n\t");
		m_forExpNum=1;
		for(j=1;j<10;j++)
		{	
			m_strEdit.Format(m_strEdit+"%d ",nodeForExp[1][j]);
			if(!(j%3))
			m_strEdit.Format(m_strEdit+"\r\n\t");
		}	
		m_strEdit.Format(m_strEdit+"\r\n\t");
	}
	int m_timesCount=0;
		
	//主体
do{
	for(i=0;i<12;i++)
	{
		tmptNode[i]=nodeForExp[1][i];
	}
	m_expNodeNum=NodeExpand(tmptNode);
	
	m_minPrice=1000000;
	for(i=1;i<=m_expNodeNum;i++)
	{
		BOOL bLabel=0;
		for(j=1;j<12;j++)
			tmptNode[j]=expNode[i][j];
		if(m_nCostChose==0){
			expNode[i][0]=CostFunction1(tmptNode);
			if(expNode[i][0]==expNode[i][10])
				bLabel=1;		
		}
		else if(m_nCostChose==1){
			expNode[i][0]=CostFunction2(tmptNode);
			if(expNode[i][0]==0)
				bLabel=1;		
		}
		else if(m_nCostChose==2){
			expNode[i][0]=CostFunction3(tmptNode);
			if(expNode[i][0]==0)
				bLabel=1;		
		}
		if(bLabel==1)
			{	
				int k=i;
				for(i=0;i<m_timesCount;i++){
					for(j=1;j<10;j++)
					{	
						m_strEdit.Format(m_strEdit+"%d ",this->nodePath[i][j]);
						if(j%3==0)
						m_strEdit.Format(m_strEdit+"\r\n\t");
					}	
						m_strEdit.Format(m_strEdit+"\r\n\t");
				}
				for(j=1;j<10;j++)
				{	
					m_strEdit.Format(m_strEdit+"%d ",end[j]);
					if((j%3==0)&&(j!=9))
					m_strEdit.Format(m_strEdit+"\r\n\t");
				}	
				m_strEdit.Format(m_strEdit+"\r\nTarget state found!"+"\r\n\t");
				m_nDepth=expNode[k][10];
				m_nTimes=++m_timesCount;
				UpdateData(FALSE);
				MessageBox("The target state found!");
				return;
			}				
		if(m_minPrice>expNode[i][0])
			m_minPrice=expNode[i][0];
	}

	if((m_forExpNum>1)&(nodeForExp[1][0]<=m_minPrice))
		for(i=1;i<m_forExpNum;i++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -