题意:给你2个 骰子,让你通过翻转使第一个变成第二个,求最少翻转数
思路:bfs
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<string> #include<map> #include<set> #include<vector> #include<queue> #include<stack> using namespace std; typedef long long ll; struct node { int arr[6]; int ci; }; int vis[1000000]; int len; bool ju(node a, node b) { for (int i = 0; i < 6; i++) if (a.arr[i] != b.arr[i]) return false; return true; } bool sol(node c) { int num = 0; for (int i = 0; i < 6; i++) { num = num * 10 + c.arr[i]; } if(vis[num]) return false; else{ vis[num] = 1; 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 main() { node p1,p2; while(scanf("%d%d%d%d%d%d",&p1.arr[0],&p1.arr[1],&p1.arr[2],&p1.arr[3],&p1.arr[4],&p1.arr[5]) != EOF) { scanf("%d%d%d%d%d%d",&p2.arr[0],&p2.arr[1],&p2.arr[2],&p2.arr[3],&p2.arr[4],&p2.arr[5]); memset(vis,0,sizeof(vis)); queue<node> q; len = p1.ci = p2.ci = 0; q.push(p1); int flag = 0; while(!q.empty()) { node tmp = q.front(); q.pop(); if(ju(tmp,p2)) { printf("%d ",tmp.ci); flag = 1; break; } for(int i = 1; i <= 4; i++) { node c = turn(tmp,i); if(sol(c)) { c.ci = tmp.ci + 1; q.push(c); } } } if(!flag) printf("-1 "); } return 0; }