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. 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遍历向前、向后、向左、向右转 ,这样如果用一个数组a[6]记录一种状态,那么最多也只有6!种状态,数量不是很多,可以直接暴力bfs。不过需要记录每个状态是否被访问过。

代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <algorithm>
  7 #include <set>
  8 #include <map>
  9 #include <queue>
 10 #include <string>
 11 #include <vector>
 12 #define inf 0x3fffffff
 13 #define esp 1e-10
 14 using namespace std;
 15 struct node1
 16 {
 17     int dice[6];
 18     int val;
 19 };
 20 struct node
 21 {
 22     node1 qt;
 23     int step;
 24 };
 25 node a;
 26 node1 b;
 27 int bfs()
 28 {
 29     set < int > s;
 30     s.insert(a.qt.val);
 31     queue < node > q;
 32     q.push(a);
 33     while (!q.empty())
 34     {
 35         node f, k;
 36         f = q.front();
 37         q.pop();
 38         if (f.qt.val == b.val) return f.step;
 39         //first
 40         k = f;
 41         swap (k.qt.dice[0], k.qt.dice[5]);
 42         swap (k.qt.dice[4], k.qt.dice[1]);
 43         swap (k.qt.dice[5], k.qt.dice[4]);
 44         k.qt.val = k.qt.dice[0];
 45         for (int y = 1; y < 6; ++y)
 46         {
 47             k.qt.val = 10*k.qt.val + k.qt.dice[y];
 48         }
 49         if (s.find(k.qt.val) == s.end())
 50         {
 51             k.step ++;
 52             q.push(k);
 53             s.insert(k.qt.val);
 54             k.step --;
 55         }
 56         //second
 57         k = f;
 58         swap (k.qt.dice[0], k.qt.dice[5]);
 59         swap (k.qt.dice[4], k.qt.dice[1]);
 60         swap (k.qt.dice[0], k.qt.dice[1]);
 61         k.qt.val = k.qt.dice[0];
 62         for (int y = 1; y < 6; ++y)
 63         {
 64             k.qt.val = 10*k.qt.val + k.qt.dice[y];
 65         }
 66         if (s.find(k.qt.val) == s.end())
 67         {
 68             k.step ++;
 69             q.push(k);
 70             s.insert(k.qt.val);
 71             k.step --;
 72         }
 73         //third
 74         k = f;
 75         swap (k.qt.dice[0], k.qt.dice[2]);
 76         swap (k.qt.dice[1], k.qt.dice[3]);
 77         swap (k.qt.dice[1], k.qt.dice[0]);
 78         k.qt.val = k.qt.dice[0];
 79         for (int y = 1; y < 6; ++y)
 80         {
 81             k.qt.val = 10*k.qt.val + k.qt.dice[y];
 82         }
 83         if (s.find(k.qt.val) == s.end())
 84         {
 85             k.step ++;
 86             q.push(k);
 87             s.insert(k.qt.val);
 88             k.step --;
 89         }
 90         //forth
 91         k = f;
 92         swap (k.qt.dice[0], k.qt.dice[2]);
 93         swap (k.qt.dice[1], k.qt.dice[3]);
 94         swap (k.qt.dice[2], k.qt.dice[3]);
 95         k.qt.val = k.qt.dice[0];
 96         for (int y = 1; y < 6; ++y)
 97         {
 98             k.qt.val = 10*k.qt.val + k.qt.dice[y];
 99         }
100         if (s.find(k.qt.val) == s.end())
101         {
102             k.step ++;
103             q.push(k);
104             s.insert(k.qt.val);
105             k.step --;
106         }
107     }
108     return -1;
109 }
110 int main()
111 {
112     //freopen ("test.txt", "r", stdin);
113     while (scanf ("%d", &a.qt.dice[0]) != EOF)
114     {
115         for (int i = 1; i < 6; ++i)
116             scanf ("%d", &a.qt.dice[i]);
117         a.step = 0;
118         a.qt.val = a.qt.dice[0];
119         for (int y = 1; y < 6; ++y)
120         {
121             a.qt.val = 10*a.qt.val + a.qt.dice[y];
122         }
123         for (int i = 0; i < 6; ++i)
124             scanf ("%d", &b.dice[i]);
125         b.val = b.dice[0];
126         for (int y = 1; y < 6; ++y)
127         {
128             b.val = 10*b.val + b.dice[y];
129         }
130         printf ("%d\n", bfs());
131     }
132     return 0;
133 }

时间: 2024-10-11 07:02:35

ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)的相关文章

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows(“?” denotes exclus

ACM学习历程—HDU 3915 Game(Nim博弈 &amp;&amp; xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所有xor和为0. 那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数. 首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的. 所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数. 对n==row特判一下. 代码:

ACM学习历程—HDU 5534 Partial Tree(动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x1)+f(x2)+...+f(xn)的最大值. 首先由于是树,所以有n-1条边,然后每条边连接两个节点,所以总的度数应该为2(n-1). 此外每个结点至少应该有一个度. 所以r1+r2+...rn = 2n-2.ri >= 1; 首先想到让ri >= 1这个条件消失: 令xi = ri,则x1+x

ACM学习历程—HDU 5536 Chip Factory(xor &amp;&amp; 字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. 此外题目中说大数据只有10组,小数据最多n只有100.(那么c*n^2的复杂度应该差不多) 于是可以考虑枚举i和j,然后匹配k. 于是可以先把所有s[k]全部存进一个字典树, 然后枚举s[i]和s[j],由于i.j.k互不相等,于是先从字典树里面删掉s[i]和s[j],然后对s[i]+s[j]这个

ACM学习历程—HDU 3949 XOR(xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的值都求出来,对于这个规模的n是不可行的. 然后之前有过类似的题,求最大的,有一种方法用到了线性基. 那么线性基能不能表示第k大的呢? 显然,因为线性基可以不重复的表示所有结果.它和原数组是等价的. 对于一个满秩矩阵 100000 010000 001000 000100 000010 000001

ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

Problem Description In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,an representing the size of the water source. Given a set of queries eac

ACM学习历程—HDU 3092 Least common multiple(数论 &amp;&amp; 动态规划 &amp;&amp; 大数)

hihoCoder挑战赛12 Description Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers