HDU - 5012 Dice(BFS)

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 <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 6;

struct Node{
	int arr[maxn], step;
	Node() {
		memset(arr, 0, sizeof(arr));
		step = 0;
	}
}start, end;
int vis[maxn*200000];

int cal(Node a) {
	int num = 0;
	for (int i = 0; i < maxn; i++) {
		num = num * 10 + a.arr[i];
	}
	return num;
}

bool equal(Node a, Node b) {
	for (int i = 0; i < maxn; i++)
		if (a.arr[i] != b.arr[i])
			return false;
	return true;
}

Node turn(Node a, int i) {
	Node c;
	if (i == 1) {
		c.arr[0] = a.arr[3];
		c.arr[1] = a.arr[2];
		c.arr[2] = a.arr[0];
		c.arr[3] = a.arr[1];
		c.arr[4] = a.arr[4];
		c.arr[5] = a.arr[5];
	}
	if (i == 2) {
		c.arr[0] = a.arr[2];
		c.arr[1] = a.arr[3];
		c.arr[2] = a.arr[1];
		c.arr[3] = a.arr[0];
		c.arr[4] = a.arr[4];
		c.arr[5] = a.arr[5];
	}
	if (i == 3) {
		c.arr[0] = a.arr[5];
		c.arr[1] = a.arr[4];
		c.arr[2] = a.arr[2];
		c.arr[3] = a.arr[3];
		c.arr[4] = a.arr[0];
		c.arr[5] = a.arr[1];
	}
	if (i == 4) {
		c.arr[0] = a.arr[4];
		c.arr[1] = a.arr[5];
		c.arr[2] = a.arr[2];
		c.arr[3] = a.arr[3];
		c.arr[4] = a.arr[1];
		c.arr[5] = a.arr[0];
	}
	return c;
}

int bfs() {
	memset(vis, 0, sizeof(vis));
	queue<Node> q;
	q.push(start);
	Node tmp;
	vis[cal(start)] = 1;
	while (!q.empty()) {
		tmp = q.front();
		q.pop();
		if (equal(tmp, end)) {
			return tmp.step;
		}
		for (int i = 1; i <= 4; i++) {
			Node c;
			c = turn(tmp, i);
			if (!vis[cal(c)]) {
				c.step = tmp.step + 1;
				vis[cal(c)] = 1;
				q.push(c);
			}
		}
	}
	return -1;
}

int main() {
	while (scanf("%d", &start.arr[0]) != EOF) {
		for (int i = 1; i < maxn; i++)
			scanf("%d", &start.arr[i]);
		for (int i = 0; i < maxn; i++)
			scanf("%d", &end.arr[i]);

		printf("%d\n", bfs());
	}
	return 0;
}
时间: 2024-10-22 06:57:32

HDU - 5012 Dice(BFS)的相关文章

HDU 5012 Dice (BFS)

其实是很水的一道bfs,用字符串表示每个状态,map判重就ok了. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5012 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cctype> #include<algorithm> #include<string> #in

HDU 1495 非常可乐 (BFS)

 问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:一个瓶子容积s,两个杯子容积分别n,m,并且都没有刻度(不能比对噢!).相互倒水,求平分的他们的最少倒水次数. 思路:暴力搜索吧.并且求最少,(即最优解),所以上BFS: 思考:状态,转移过程,怎么剪纸. 惨痛的debug,我不解释了. 源代码: #include<iostream> #include<cstdio> #include<queue> #

ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)

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.

HDU 1495 非常可乐(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有

HDU 1175 连连看(BFS)

Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去.不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的.现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过.玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能

hdu 1401 双向搜索(bfs)

题意是给你两个棋盘格局   问由第一个最多搜8步能不能到达第二个格局: 单项广搜应该能做    注意优化就行   我是想练一下双向  所以这里就只说下双向的吧: 和普通搜索一样    但由于起点终点的地位一样 (及起点也可以是终点,不要求求最小步数)  这样就可以让起点和终点各走4步   看有没有遇到的地方: 注意  这里的4个棋子都是一样的,导致你标记的时候不能简单的直接进行标记   (为此贡献了几次wa   唉  只怪当初太年轻)   我的处理上把4个棋子进行排序然后进行标记   标记数组开

HDU 1072 Nightmare(BFS)

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9120    Accepted Submission(s): 4389 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

HDU 4652 Dice (概率DP)

Dice Problem Description You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form: 0