(HDU5012)Dice-BFS

Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2310    Accepted Submission(s): 1154


 

Problem Description

There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6 to be numbers on specific faces of dice B. It’s guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while ai ≠ aj and bi ≠ bj for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7.

At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)


Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.

 

Input

There are multiple test cases. Please process till EOF.

For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.

The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.

 

Output

For each test case, print a line with a number representing the answer. If there’s no way to make two dices exactly the same, output -1.

 

Sample Input

 

1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 6 4 3 1 2 3 4 5 6 1 4 2 5 3 6

 

Sample Output

 

0 3 -1

题目分析:

题目大意是给两行数,一行代表一个立方体,每行数按顺序依次代表立方体前后左右上下面上的值。有四种操作,及分别以地面矩形的四条边为轴将立方体放倒,问最少操作多少次可以让第二个立方体和第一个立方体相同(两个立方体对应面上的数字相同)。很明显,给定了初始状态和结束状态问最小的代价,所以用BFS。

代码:

#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
struct Node {
	int p[6];
	int step;
	bool operator==(const Node a)const {
		for (int i = 0; i < 6; i++) {
			if (this->p[i] != a.p[i])return false;
		}
		return true;
	}
};
queue<Node> q;
Node da;
Node db;
int visit[7][7][7][7][7][7];
int dfs() {
	while (!q.empty())q.pop();
	db.step = 0;
	q.push(db);
	Node temp;
	while (!q.empty())
	{
		temp = q.front();
		q.pop();
		visit[temp.p[0]][temp.p[1]][temp.p[2]][temp.p[3]][temp.p[4]][temp.p[5]] = 1;
		//结束条件
		if (temp == da)return temp.step;
		Node t;
		t = temp;
		//以下为四种不同的操作
		temp.p[0] = t.p[2];
		temp.p[1] = t.p[3];
		temp.p[2] = t.p[1];
		temp.p[3] = t.p[0];
		temp.step++;
		if(!visit[temp.p[0]][temp.p[1]][temp.p[2]][temp.p[3]][temp.p[4]][temp.p[5]])
		q.push(temp);

		temp = t;
		temp.p[1] = t.p[2];
		temp.p[0] = t.p[3];
		temp.p[3] = t.p[1];
		temp.p[2] = t.p[0];
		temp.step++;
		if (!visit[temp.p[0]][temp.p[1]][temp.p[2]][temp.p[3]][temp.p[4]][temp.p[5]])
		q.push(temp);

		temp = t;
		temp.p[0] = t.p[4];
		temp.p[1] = t.p[5];
		temp.p[4] = t.p[1];
		temp.p[5] = t.p[0];
		temp.step++;
		if (!visit[temp.p[0]][temp.p[1]][temp.p[2]][temp.p[3]][temp.p[4]][temp.p[5]])
		q.push(temp);

		temp = t;
		temp.p[1] = t.p[4];
		temp.p[0] = t.p[5];
		temp.p[5] = t.p[1];
		temp.p[4] = t.p[0];
		temp.step++;
		if (!visit[temp.p[0]][temp.p[1]][temp.p[2]][temp.p[3]][temp.p[4]][temp.p[5]])
		q.push(temp);
	}
	return -1;
}
int main() {
	while (cin >> da.p[0] >> da.p[1] >> da.p[2] >> da.p[3] >> da.p[4] >> da.p[5] >> db.p[0] >> db.p[1] >> db.p[2] >> db.p[3] >> db.p[4] >> db.p[5])
	{
		if (da==db) {
			cout << 0 << endl;
			continue;
		}
		memset(visit, 0, sizeof(visit));
		cout << dfs() << endl;
	}
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/xiaosi1524/article/details/81585593
今日推荐