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

📄 bdfs.cpp

📁 滑块问题求解系统:利用深度优先搜索和广度优先搜索解决有趣的滑块问题求解系统。
💻 CPP
字号:
#include "stdafx.h"
#include "bdfs.h"


HASH_BDFS tree_bdfs;

CString BDFS::getIntroduction(){
	CString intro = "有界深度优先搜索\r\n\r\n"
					"搜索时优先搜索子树,"
					"但是不能保证找到最优解。"
					"由于有深度限制,所以可以保证不会陷入一棵子树内出不来,"
					"但是却有可能因为深度不够而找不到解。";
	return intro;
}

CString BDFS::getResult(){
	CString str;
	str = searcher::getResult();
	str += "\r\n";
	char ss[20];
	sprintf(ss, "当前深度限制:%d", maxstep);
	return (str += ss);	
}



int BDFS::search(int begin, int end, int &stop, int val){
	if((getreverse(begin) & 1) != (getreverse(end) & 1) ){  //逆序数不一样,无解
		result = NOANSWER;
		return -1;
	}
	maxstep = val;
	std::vector<int> Q;
	result = SUCCESS;
	open[0].father = -1;
	open[0].step = 0;
	open[0].status = begin;
	int index, tmp;
	for(index = 0, tmp = begin; tmp % 10; ++index, tmp /= 10);
	open[0].zero = index;
	Q.push_back(top = 0);
	tree_bdfs.reset();
	tree_bdfs.add(begin, 0, 0);
	reset(begin, end); //初始转换信息
	while(!stop && !Q.empty()){
		index = Q.back();
		Q.pop_back();		
		if(open[index].status == end){
			last_index = index;
			totalnode = top;
			expanded = top + 1 - Q.size();
			inopen = Q.size();
			return 1;
		}
		if(open[index].step >= val) continue;
		int zero = open[index].zero;
		for(int i = 0; i < dir_count[zero]; i++){				
			int tmp = getnextstatus(open[index].status, zero, dir[zero][i]);
			int newindex = tree_bdfs.add(tmp, top + 1, open[index].step + 1);
			if(newindex == 0){
				open[++top].father = index;
				open[top].status = tmp;
				open[top].step = open[index].step + 1;
				open[top].zero = dir[zero][i];
				Q.push_back(top);
			}else if(newindex > 0){
				open[newindex].father = index;
				open[newindex].step = open[index].step + 1;			
				Q.push_back(newindex);
			}
		}
	}

	expanded = top + 1 - Q.size();
	inopen = Q.size();
	if(stop){
		result = STOP;
		return -2;
	}
	result = CANTFIND;
	return 0;
}


⌨️ 快捷键说明

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