compare_first.cpp

来自「滑块问题求解系统:利用深度优先搜索和广度优先搜索解决有趣的滑块问题求解系统。」· C++ 代码 · 共 108 行

CPP
108
字号
// Compare.cpp : 实现文件
//

#include "stdafx.h"
#include "AI.h"
#include "Compare_first.h"
#include <cmath>
#include ".\Compare_first.h"


// CCompare_first 对话框

IMPLEMENT_DYNAMIC(CCompare_first, CDialog)
CCompare_first::CCompare_first(CWnd* pParent /*=NULL*/)
	: CDialog(CCompare_first::IDD, pParent)
{
	comp[1].search = &dfs;
	comp[2].search = &bdfs;	//有界
	comp[3].search = &bfs;
	comp[4].search = &tbfs;
	comp[5].search = &astar1;  //启发
	comp[6].search = &astar2;
	comp[7].search = &idfs;
	comp[8].search = &ida;
	for(int i = 1, j = IDC_STEP_1, m = IDC_NODE_1, k = IDC_KEY_1; i <= 8; ++i, ++j, ++m, ++k){
		comp[i].step_item = j;
		comp[i].node_item = m;
		comp[i].key_item = k;
	}	
}

CCompare_first::~CCompare_first()
{
}

void CCompare_first::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CCompare_first, CDialog)
	ON_BN_CLICKED(IDC_COMP_SEARCH, OnBnClickedCompSearch)
END_MESSAGE_MAP()


// CCompare_first 消息处理程序


//线程执行函数:

DWORD WINAPI CompSearchProc(LPVOID lpParameter){
	CCompare_first *p = (CCompare_first *) lpParameter;

	p->stop = 0;

	//搜索、显示
	for(int i = 1; i <= 8 && !p->stop; ++i){
		int ans;
		if(i == 2)
			ans = p->comp[i].search->search(p->m_begin, p->m_end, p->stop, p->m_maxstep);
		else
			ans = p->comp[i].search->search(p->m_begin, p->m_end, p->stop, p->m_heuristic);

		if(ans == -1 ){
			p->MessageBox("此图无解!");	
			break;
		}else if(ans == -2){
			p->MessageBox("用户终止搜索!");		
			break;
		}else if(!ans){   //有解找不到解
			p->SetDlgItemText(p->comp[i].step_item, "未得解");
			p->SetDlgItemText(p->comp[i].node_item, "未得解");
			p->SetDlgItemText(p->comp[i].key_item, "未得解");
		}else{		
			p->SetDlgItemInt(p->comp[i].step_item, p->comp[i].search->getStep() );
			p->SetDlgItemInt(p->comp[i].node_item, p->comp[i].search->getTotalnode() );
			char s[10];
			double B = p->comp[i].search->getB();
			if(fabs(B - 1.0) < 1e-2)
				sprintf(s, "难求解");
			else
				sprintf(s, "%.3lf", B );
			p->SetDlgItemText(p->comp[i].key_item, s);
		}
	}
	
	p->SetDlgItemText(IDC_COMP_SEARCH, "开始搜索");
	
	return 0;
}


void CCompare_first::OnBnClickedCompSearch()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	if(GetDlgItemText(IDC_COMP_SEARCH, str), str == "停止搜索"){
		stop = 1;
		return;
	}

	SetDlgItemText(IDC_COMP_SEARCH, "停止搜索");

	HANDLE hThread1 = CreateThread(NULL, 0, CompSearchProc, this, 0, NULL);
	CloseHandle(hThread1);
}

⌨️ 快捷键说明

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