题目大意:给定两个骰子,要按照只能左右前后的翻转方式,将两个骰子摆成状态一直的顺序,问最少需要多少步?
解题思路:不知道是喜还是忧,名次上升了,哈哈哈哈,真的只有一步之遥了!心情大好,接着发下昨天的题解吧!这个题是一个搜索题,很简单的,只需要将搜索的四个方向换成四种状态。就这一点的改变,其它就不赘述了,详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5012
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 10;
int a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6;
int flag;
int vis[N][N][N][N][N][N];
struct node{
int x1,x2,x3,x4,x5,x6,step;
};
int bfs(){
node s,t;
queue<node> q;
s.x1=a1;s.x2=a2;s.x3=a3;
s.x4=a4;s.x5=a5;s.x6=a6;
s.step=0;
q.push(s);
while(!q.empty()){
s=q.front();q.pop();
if(s.x1==b1 && s.x2==b2 && s.x3==b3 && s.x4==b4 && s.x5==b5 && s.x6==b6) return s.step;
for(int i=0;i<4;i++){ //四种状态的转换
if(i==0){t.x1=s.x3;t.x2=s.x4;t.x3=s.x2;t.x4=s.x1;t.x5=s.x5;t.x6=s.x6;}
else if(i==1){t.x1=s.x5;t.x2=s.x6;t.x3=s.x3;t.x4=s.x4;t.x5=s.x2;t.x6=s.x1;}
else if(i==2){t.x1=s.x4;t.x2=s.x3;t.x3=s.x1;t.x4=s.x2;t.x5=s.x5;t.x6=s.x6;}
else{t.x1=s.x6;t.x2=s.x5;t.x3=s.x3;t.x4=s.x4;t.x5=s.x1;t.x6=s.x2;}
if(!vis[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]){
t.step=s.step+1;
vis[t.x1][t.x2][t.x3][t.x4][t.x5][t.x6]=1;
q.push(t);
}
}
}
return -1;
}
int main(){
//freopen("input.txt","r",stdin);
while(scanf("%d%d%d%d%d%d%d%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5,&a6,&b1,&b2,&b3,&b4,&b5,&b6)!=EOF){
flag=0;
memset(vis,0,sizeof(vis));
int ans=bfs();
if(ans!=-1) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}
再补上一个队友的code:
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
int start[6], target[6];
int dis[6][6][6][6][6][6];
struct State {
int top, bot, left, right, front, back;
State() {}
State(int a, int b, int c, int d, int e, int f):
top(a), bot(b), left(c), right(d), front(e), back(f) {}
bool is_target() {
return top == target[0] &&
bot == target[1] &&
left == target[2] &&
right == target[3] &&
front == target[4] &&
back == target[5];
}
bool is_visited() {
return dis[top][bot][left][right][front][back] != -1;
}
void mark(int _v) {
dis[top][bot][left][right][front][back] = _v;
}
int get_mark() {
return dis[top][bot][left][right][front][back];
}
void lrot() {
int _t = top;
top = right;
right = bot;
bot = left;
left = _t;
}
void rrot() {
int _t = top;
top = left;
left = bot;
bot = right;
right = _t;
}
void frot() {
int _t = top;
top = back;
back = bot;
bot = front;
front = _t;
}
void brot() {
int _t = top;
top = front;
front = bot;
bot = back;
back = _t;
}
};
queue<State> que;
int work() {
queue<State> new_que;
que = new_que;
fill(dis[0][0][0][0][0], dis[6][0][0][0][0], -1);
State u(start[0], start[1], start[2], start[3], start[4], start[5]);
State v;
u.mark(0);
que.push(u);
while (!que.empty()) {
u = que.front();
que.pop();
if (u.is_target()) return u.get_mark();
v = u;
v.lrot();
if (!v.is_visited()) {
v.mark(u.get_mark() + 1);
que.push(v);
}
v = u;
v.rrot();
if (!v.is_visited()) {
v.mark(u.get_mark() + 1);
que.push(v);
}
v = u;
v.frot();
if (!v.is_visited()) {
v.mark(u.get_mark() + 1);
que.push(v);
}
v = u;
v.brot();
if (!v.is_visited()) {
v.mark(u.get_mark() + 1);
que.push(v);
}
}
return -1;
}
int main() {
freopen("input.txt","r",stdin);
while (~scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &start[0], &start[1], &start[2], &start[3], &start[4], &start[5], &target[0], &target[1], &target[2], &target[3], &target[4], &target[5])) {
for (int i = 0; i < 6; ++i) {
start[i] -= 1;
target[i] -= 1;
}
printf("%d\n", work());
}
return 0;
}