BFS + 剪枝 之 hdu 5012 Dice

/*
BFS 遍历所有状态,当寻找到要转到的状态,输出BFS的层数,即答案,否则输出 -1;
注意:
剪枝: 当BFS到某一种状态时,若此状态已在前面所寻找的状态中出现过,则不用再继续从此状态BFS了,因为此状态所能寻找的状态已经       在之前都处理过了。
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <map>
using namespace std;
struct myNode {
	int Top, Bottom, Left, Right, Front, Back;
};
typedef pair<myNode, int> P;

myNode node[2];
vector<myNode> vec;

void myDeal(int myCase, myNode myNd, myNode &t_Nd) {
	// top face, bottom face, left face, right face, front face and back face
	switch (myCase) {
	case 1: {
		t_Nd.Top = myNd.Right;
		t_Nd.Bottom = myNd.Left;
		t_Nd.Left = myNd.Top;
		t_Nd.Right = myNd.Bottom;
		t_Nd.Front = myNd.Front;
		t_Nd.Back = myNd.Back;
		return;
	}
	case 2: {
		t_Nd.Top = myNd.Left;
		t_Nd.Bottom = myNd.Right;
		t_Nd.Left = myNd.Bottom;
		t_Nd.Right = myNd.Top;
		t_Nd.Front = myNd.Front;
		t_Nd.Back = myNd.Back;
		return;
	}
	case 3: {
		t_Nd.Top = myNd.Back;
		t_Nd.Bottom = myNd.Front;
		t_Nd.Front = myNd.Top;
		t_Nd.Back = myNd.Bottom;
		t_Nd.Left = myNd.Left;
		t_Nd.Right = myNd.Right;
		return;
	}
	case 4: {
		t_Nd.Top = myNd.Front;
		t_Nd.Bottom = myNd.Back;
		t_Nd.Front = myNd.Bottom;
		t_Nd.Back = myNd.Top;
		t_Nd.Left = myNd.Left;
		t_Nd.Right = myNd.Right;
		return;
	}
	default:
		system("pause");
		return;
	}
}

bool myJudge(const myNode n1, const myNode n2) {
	if ((n1.Top == n2.Top) && (n1.Bottom == n2.Bottom) && (n1.Front == n2.Front) &&
		(n1.Back == n2.Back) && (n1.Left == n2.Left) && (n1.Right == n2.Right)) {
		return true;
	}
	return false;
}

bool Judge(myNode nd) {
	for (int i = 0; i < vec.size(); ++i) {
		if (myJudge(vec[i], nd)) {
			return true;
		}
	}
	return false;
}

void Solve() {
	queue<P> que;
	que.push(P(node[0], 0));
	while (!que.empty()) {
		P p1 = que.front();
		if (Judge(p1.first)){
			//cout << "Yes" << endl;
			que.pop();
			continue;
		}
		vec.push_back(p1.first);
		que.pop();
		if (myJudge(p1.first, node[1])) {
			printf("%d\n", p1.second);
			return;
		}
		myNode t_Nd;
		for (int i = 1; i <= 4; ++i) {
			myDeal(i, p1.first, t_Nd);
			//cout << t_Nd.Top << " " << t_Nd.Bottom << " " << t_Nd.Left << " " << t_Nd.Right << " " << t_Nd.Front << " " << t_Nd.Back << endl;
			que.push(P(t_Nd, p1.second + 1));
		}
	}
	printf("-1\n");
}

int main() {
	//freopen("input.txt", "r", stdin);
	int n;
	while (~scanf("%d %d %d %d %d %d", &node[0].Top, &node[0].Bottom, &node[0].Left, &node[0].Right, &node[0].Front, &node[0].Back)) {
		scanf("%d %d %d %d %d %d", &node[1].Top, &node[1].Bottom, &node[1].Left, &node[1].Right, &node[1].Front, &node[1].Back);
		Solve();
		if (!vec.empty()) vec.clear();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值