Zobrist Hash算法的基础:按位异或运算的性质

用途

Zobrist Hash算法是各类棋类程序中判断历史局面是否存在的算法。


算法基础

该算法的的基础利用了按位异或运算的如下性质:A^B^B = A


C++代码示例

// mersenne_twister_engine constructor  
#include   
#include   
#include   
#include 

using namespace std;

int main()
{
	// obtain a seed from the system clock:  
	unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();

	// obtain a seed from the user:  

	std::mt19937 g1(seed1);  // mt19937 is a standard mersenne_twister_engine  
	std::cout << "A time seed produced: " << g1() << std::endl;
	std::cout << typeid(g1()).name() << " sizeof(" << typeid(g1()).name() << ") = " << sizeof(g1()) << endl;

	list listInt;

	int current = 0;
	cout << "current=" << current << endl;

	for (int i = 0; i < 10; ++i)
	{
		int temp = g1();
		current ^= temp;
		listInt.push_back(temp);
		cout << "temp=" << temp << " current=" << current << endl;
	}
	cout << endl;
	for (auto itr = listInt.crbegin(); itr != listInt.crend(); ++itr)
	{
		int temp = *itr;
		cout << "temp=" << temp << " current=" << current << endl;
		current ^= temp;
	}
	cout << "current=" << current << endl;

	return 0;
}

运行结果

Zobrist Hash算法的基础:按位异或运算的性质_第1张图片

你可能感兴趣的:(C++,11,中国象棋,C++,中国象棋系列)