【HDU - 5012】Dice(模拟,bfs)
題干:
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a?1.a?2,a?3,a?4,a?5,a?6?to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b?1.b?2,b?3,b?4,b?5,b?6?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 a?i?≠ a?j?and b?i?≠ b?j?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, a?i?≠ b?i). Ddy wants to make the two dices look the same from all directions(which means for all i, a?i?= b?i) 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 a?1,a?2,a?3,a?4,a?5,a?6, representing the numbers on dice A.
The second line consists of six integers b?1,b?2,b?3,b?4,b?5,b?6, 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 6Sample Output
0 3 -1題目大意:
給出兩個正方體,每個正方體都有6種顏色,問是否可以經過左轉,右轉,前轉,后轉四種轉法讓兩個正方體看起來一樣(對應面顏色相同),最少轉幾次。
解題報告:
?直接bfs。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; vector<int> aa; set<vector<int> > ss; struct Node {vector<int> vv;int s; }; int bfs(vector<int> st) {queue<Node> q;q.push({st,0});while(q.size()) {Node cur = q.front();q.pop();if(cur.vv == aa) return cur.s;if(ss.count(cur.vv)) continue;ss.insert(cur.vv);vector<int> t = cur.vv,nn(6);//Leftnn[0] = t[3],nn[1] = t[2],nn[2] = t[0],nn[3] = t[1],nn[4] = t[4],nn[5] = t[5];q.push({nn,cur.s+1});//Right nn[0] = t[2],nn[1] = t[3],nn[2] = t[1],nn[3] = t[0],nn[4] = t[4],nn[5] = t[5];q.push({nn,cur.s+1});//Frontnn[0] = t[5],nn[1] = t[4],nn[2] = t[2],nn[3] = t[3],nn[4] = t[0],nn[5] = t[1];q.push({nn,cur.s+1});//Backnn[0] = t[4],nn[1] = t[5],nn[2] = t[2],nn[3] = t[3],nn[4] = t[1],nn[5] = t[0];q.push({nn,cur.s+1});}return -1; } int main() {int z,x,c,v,b,n;while(~scanf("%d%d%d%d%d%d",&z,&x,&c,&v,&b,&n)) {aa.clear(); ss.clear();vector<int> a(6);a[0]=z;a[1]=x;a[2]=c;a[3]=v;a[4]=b;a[5]=n;for(int i = 1; i<=6; i++) {scanf("%d",&x);aa.pb(x);}int ans = bfs(a);printf("%d\n",ans);} return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 5012】Dice(模拟,bfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软Edge自卖自夸:号称最适合玩家的浏
- 下一篇: php有哪些开源社区,PHP开源社区