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

📄 applyhousehint.cpp

📁 爱因斯坦谜题
💻 CPP
字号:
// ApplyHouseHint.cpp: implementation of the CApplyHouseHint class.
//
//////////////////////////////////////////////////////////////////////

#include "ApplyHouseHint.h"

bool CApplyHouseHint::m_house_enum = false;

void CApplyHouseHint::filter_out(const CSolutionSet &in_set, CSolutionSet &out_set)
{
	int in_cnt = in_set.GetSize();
	CSolution sln;

	for (int i=0; i<in_cnt; i++) {
		int h1, h2;
		sln = in_set.GetAt(i);
		h1 = sln.GetHouse(m_hint.v1);
		h2 = sln.GetHouse(m_hint.v2);
		if (!h1 || !h2) {
			continue;
		}
		if (IsHousesOk(h1,h2)) {
			out_set.Add(sln);
		}
	}
}

void CApplyHouseHint::ApplyHint(const CSolution &sln, CSolutionSet &output)
{
	CSolutionSet in_set, out_set;

	in_set.Add(sln);

	// 保证需要检查的属性没有“未知项”
	enum_attr(in_set, out_set, m_hint.v1);
	in_set = out_set;
	enum_attr(in_set, out_set, m_hint.v2);

	// 保证House属性没有“未知项”
	if (!m_house_enum) {
		in_set = out_set;
		enum_house(in_set, out_set);
	}

	// 检查输入集合,去掉不满足当前hint的方案
	filter_out(out_set, output);
}

void CApplyHouseHint::Apply(const CSolutionSet &input, CSolutionSet &output)
{
	CApplyHint::Apply(input, output);
	m_house_enum = true;
}

void CApplyHouseHint::enum_attr(const CSolutionSet &in_set, CSolutionSet &out_set, CAttributeVal attr)
{
	int in_cnt = in_set.GetSize();

	if (in_set.GetAt(0).HasAttrVal(attr)) {
		out_set = in_set;
		return;
	}

	out_set.RemoveAll();
	for (int i=0; i<in_cnt; i++) {
		in_set.GetAt(i).EnumAttr(attr, out_set);
	}
}

void CApplyHouseHint::enum_house(const CSolutionSet &in_set, CSolutionSet &out_set)
{
	CAttributeVal attr_val;
	int val;
	CSolutionSet set;

	attr_val.attr = ORDERED_ATTR;
	set = in_set;
	for (val=1;val<=5;val++) {
		attr_val.val = val;
		enum_attr(set, out_set, attr_val);
		set = out_set;
	}
}

bool CApplyNeighborHint::IsHousesOk(int h1, int h2)
{
	return ((h1-h2==1)||(h2-h1==1));
}

bool CApplyOrderedNeighborHint::IsHousesOk(int h1, int h2)
{
	return (h2-h1==1);
}

⌨️ 快捷键说明

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