HDU 5012 bfs暴搜

Dice

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

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

题目意思:

一个正方形,每个面有一个数字而且不相同(最多为6最小为1),正方形可以向左、右、前、后转,给出正方形两个状态,问最少转多少次可以从前面的状态转换成后面的状态,若不能输出-1。

思路:

正方形最多有6!种状态,由前面状态推到最后的状态,很明显bfs暴搜,和迷宫问题差不多。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <queue>
 6 using namespace std;
 7
 8 struct node{
 9     int a[7], d;
10 };
11
12 int visited[700000];
13
14 int bfs(int x[],int y[]){
15     queue<node>Q;
16     node p, q;
17     int num=0;
18     p.a[1]=x[1];p.a[2]=x[2];p.a[3]=x[3];p.a[4]=x[4];p.a[5]=x[5];p.a[6]=x[6];p.d=0;
19     visited[x[1]*100000+x[2]*10000+x[3]*1000+x[4]*100+x[5]*10+x[6]]=1;
20     Q.push(p);
21     while(!Q.empty()){
22         p=Q.front();
23         Q.pop();
24         int f=1;
25         for(int i=1;i<=6;i++){
26             if(p.a[i]!=y[i]){
27                 f=0;break;
28             }
29         }
30         if(f) return p.d;
31         //向左
32          q.a[1]=p.a[4];q.a[2]=p.a[3];q.a[3]=p.a[1];q.a[4]=p.a[2];q.a[5]=p.a[5];q.a[6]=p.a[6];
33          if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){
34              visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1;
35              q.d=p.d+1;
36              Q.push(q);
37          }
38          //向右
39           q.a[1]=p.a[3];q.a[2]=p.a[4];q.a[3]=p.a[2];q.a[4]=p.a[1];q.a[5]=p.a[5];q.a[6]=p.a[6];
40           if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){
41               visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1;
42              q.d=p.d+1;
43              Q.push(q);
44          }
45          //向前
46           q.a[1]=p.a[6];q.a[2]=p.a[5];q.a[3]=p.a[3];q.a[4]=p.a[4];q.a[5]=p.a[1];q.a[6]=p.a[2];
47           if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){
48               visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1;
49              q.d=p.d+1;
50              Q.push(q);
51          }
52          //向后
53           q.a[1]=p.a[5];q.a[2]=p.a[6];q.a[3]=p.a[3];q.a[4]=p.a[4];q.a[5]=p.a[2];q.a[6]=p.a[1];
54           if(!visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]){
55               visited[q.a[1]*100000+q.a[2]*10000+q.a[3]*1000+q.a[4]*100+q.a[5]*10+q.a[6]]=1;
56              q.d=p.d+1;
57              Q.push(q);
58          }
59     }
60     return -1;
61 }
62 main()
63 {
64     int a[7], b[7];
65     int i, j, k;
66     while(scanf("%d",&a[1])==1){
67         memset(visited,0,sizeof(visited));
68         for(i=2;i<=6;i++) scanf("%d",&a[i]);
69         for(i=1;i<=6;i++) scanf("%d",&b[i]);
70         printf("%d\n",bfs(a,b));
71     }
72 }
时间: 2024-10-05 03:35:48

HDU 5012 bfs暴搜的相关文章

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110    Accepted Submission(s): 280 Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all peo

2014牡丹江网络预选赛F题(隐式图BFS暴搜)zoj3814

Sawtooth Puzzle Time Limit: 10 Seconds      Memory Limit: 65536 KB Recently, you found an interesting game called Sawtooth Puzzle. This is a single-player game played on a grid with 3 x 3 cells. Each cell contains a part of an image. Besides, each ed

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

CF Gym Dice Game BFS 暴搜

Time limit  1000 ms Memory limit  262144 kB A dice is a small cube, with each side having a different number of spots on it, ranging from 1 to 6. Each side in the dice has 4 adjacent sides that can be reached by rotating the dice (i.e. the current si

hdu 5012 bfs --- 慎用STL 比如MAP判重

http://acm.hdu.edu.cn/showproblem.php?pid=5012 发现一个问题 如果Sting s = '1'+'2'+'3'; s!="123"!!!!!!  而是有乱码 先贴一份自己的TLE 代码, 超时应该是因为: 1.cin 2.map判重 map find太花时间 3.string花时间 4.其实不用两个都旋转,只要旋转一个就行,这样可以省下很多时间 包括少用了make_pair pair的判重等等....哎  二笔了  太暴力了 #include

hdu 5012 bfs 康托展开

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 491    Accepted Submission(s): 290 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number w

HDU 5012 BFS水

2014 ACM/ICPC Asia Regional Xi'an Online 对于一个筛子,规定了以底面的四个边为轴,可以进行翻转,给出起始状态,求最少步骤到目标状态. 简单BFS #include "stdio.h" #include "string.h" #include "math.h" #include "queue" using namespace std; struct node { int s[7]; int

HDU 5339 Untitled(暴搜)

Untitled Problem Description There is an integer $a$ and $n$ integers $b_1, \ldots, b_n$. After selecting some numbers from $b_1, \ldots, b_n$ in any order, say $c_1, \ldots, c_r$, we want to make sure that $a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ m

[HDU 5135] Little Zu Chongzhi&#39;s Triangles (dfs暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边从小到大排序,这样前面的两条边加起来如果不大于第三条边就可以跳出,这是一个存在性条件. dfs(int idx,int now,int cnt,int nowmax)代表我当前处理的是第idx条边,已经加入边集的有cnt条边,当前的边的长度和为now,组成的最大面积和为nowmax. 暴力枚举每个三