设为首页 加入收藏

TOP

HDU 5012 Dice (bfs)
2015-07-20 17:41:41 来源: 作者: 【 】 浏览:1
Tags:HDU 5012 Dice bfs

Dice

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


Problem Description 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<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3ViPixhPHN1Yj4yPC9zdWI+LGE8c3ViPjM8L3N1Yj4sYTxzdWI+NDwvc3ViPixhPHN1Yj41PC9zdWI+LGE8c3ViPjY8L3N1Yj4sIHJlcHJlc2VudGluZyB0aGUgbnVtYmVycyBvbiBkaWNlIEEuCjxicj4KPGJyPgpUaGUgc2Vjb25kIGxpbmUgY29uc2lzdHMgb2Ygc2l4IGludGVnZXJzIGI8c3ViPjE8L3N1Yj4sYjxzdWI+Mjwvc3ViPixiPHN1Yj4zPC9zdWI+LGI8c3ViPjQ8L3N1Yj4sYjxzdWI+NTwvc3ViPixiPHN1Yj42PC9zdWI+LCByZXByZXNlbnRpbmcgdGhlIG51bWJlcnMgb24gZGljZSBCLgogCjxicj4KT3V0cHV0CkZvciBlYWNoIHRlc3QgY2FzZSwgcHJpbnQgYSBsaW5lIHdpdGggYSBudW1iZXIgcmVwcmVzZW50aW5nIHRoZSBhbnN3ZXIuIElmIHRoZXJloa9zIG5vIHdheSB0byBtYWtlIHR3byBkaWNlcyBleGFjdGx5IHRoZSBzYW1lLCBvdXRwdXQgLTEuCiAKPGJyPgpTYW1wbGUgSW5wdXQKCjxwcmUgY2xhc3M9"brush:java;">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

Source 2014 ACM/ICPC Asia Regional Xi'an Online
Recommend


#include
   
    
#include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) using namespace std; #define N 700005 struct stud{ int a1,a2,a3,a4,a5,a0; int time; }s,e,t; int ans; int vis[N]; int fdd(struct stud x) { int temp=0; temp+=x.a0*1; temp+=x.a1*7; temp+=x.a2*7*7; temp+=x.a3*7*7*7; temp+=x.a4*7*7*7*7; temp+=x.a5*7*7*7*7*7; return temp; } stud fddd(stud cur,int i) { struct stud next; if(i==0) { next.a0=cur.a2; next.a1=cur.a3; next.a2=cur.a1; next.a3=cur.a0; next.a4=cur.a4; next.a5=cur.a5; } else if(i==1) { next.a0=cur.a3; next.a1=cur.a2; next.a2=cur.a0; next.a3=cur.a1; next.a4=cur.a4; next.a5=cur.a5; } else if(i==2) { next.a0=cur.a5; next.a1=cur.a4; next.a2=cur.a2; next.a3=cur.a3; next.a4=cur.a0; next.a5=cur.a1; } else { next.a0=cur.a4; next.a1=cur.a5; next.a2=cur.a2; next.a3=cur.a3; next.a4=cur.a1; next.a5=cur.a0; } return next; } int bfs() { int temp,i; struct stud cur,next; memset(vis,0,sizeof(vis)); temp=fdd(s); ans=fdd(e); vis[temp]=1; cur=s; cur.time=0; queue
         
          q; q.push(cur); while(!q.empty()) { cur=q.front(); q.pop(); if(fdd(cur)==ans) return cur.time; for(i=0;i<4;i++) //旋转 { stud t=fddd(cur,i); temp=fdd(t); if(!vis[temp]) { t.time=cur.time+1; q.push(t); vis[temp]=1; } } } return -1; } int main() { int i; while(~scanf("%d",&s.a0)) { scanf("%d%d%d%d%d",&s.a1,&s.a2,&s.a3,&s.a4,&s.a5); scanf("%d%d%d%d%d%d",&e.a0,&e.a1,&e.a2,&e.a3,&e.a4,&e.a5); printf("%d\n",bfs()); } } 
         
        
       
      
     
    
   




】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 5015 233 Matrix(西安网络赛.. 下一篇数据结构与算法问题 [NOIP2001]求..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·MySQL 安装及连接-腾 (2025-12-25 06:20:28)
·MySQL的下载、安装、 (2025-12-25 06:20:26)
·MySQL 中文网:探索 (2025-12-25 06:20:23)
·Shell脚本:Linux Sh (2025-12-25 05:50:11)
·VMware虚拟机安装Lin (2025-12-25 05:50:08)