每次模拟一下骰子的翻转,然后用bfs
简单bfs,一直wa,只好找了北大的重写一遍
用vector记录节点信息,用map映射节点和步数
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 1000000000
#define maxn
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define mset(x) memset(x,0,sizeof(x))
typedef __int64 ll;
vector<int> s, t;
map<vector<int>, int> m;
queue< vector<int> > q;
const int trans[4][6] = {
{2, 3, 1, 0, 4, 5},
{3, 2, 0, 1, 4, 5},
{4, 5, 2, 3, 1, 0},
{5, 4, 2, 3, 0, 1}
};
int bfs(){
if(s==t) return 0;
while(q.size()) q.pop(); //q没有clear
m.clear();
m[s] = 0;
q.push(s);
while(q.size()){
vector<int> u = q.front();
q.pop();
int d=m[u];
vector<int> v; v.resize(6);
for(int i=0; i<4; i++){
for(int j=0; j<6; j++)
v[j] = u[trans[i][j]];
if(!m.count(v)){
if(v==t) return d+1;
m[v] = d+1;
q.push(v);
}
}
}
return -1;
}
int main(){
// freopen("a.txt","r",stdin);
// freopen(".out","w",stdout);
int x;
while(scanf("%d",&x)!=EOF){
s.resize(6); t.resize(6);
s[0] = x;
for(int i=1;i<6;i++) scanf("%d", &s[i]);
for(int i=0;i<6;i++) scanf("%d", &t[i]);
cout<<bfs()<<endl;
}
return 0;
}
/*
DESCRIPTION:
*/