ZOJ 3736 Pocket Cube

1、本题就是一道隐式图搜索。

2、不需要判重,因为可以重复,也不需要规定每次一定要转动,因为不转的话,答案不可能比转动更优。

3、把六种旋转情况考虑出来即可。

#include<cstdio>
#include<queue>
using namespace std;
struct cube{
    int d[24];
 int cnt;
};
cube c;
int f[6][24]={
    {0,21,2,23,4,5,6,1,9,15,10,11,12,3,8,14,16,7,18,13,20,17,22,19},
    {0,7,2,13,4,5,6,17,14,8,10,11,12,19,15,9,16,21,18,23,20,1,22,3}, 
    {2,0,3,1,6,7,8,9,23,22,10,11,12,13,14,15,16,17,18,19,20,21,5,4}, 
    {1,3,0,2,23,22,4,5,6,7,10,11,12,13,14,15,16,17,18,19,20,21,9,8}, 
    {0,1,11,5,4,16,12,6,2,9,10,17,13,7,3,15,14,8,18,19,20,21,22,23}, 
    {0,1,8,14,4,3,7,13,17,9,10,2,6,12,16,15,5,11,18,19,20,21,22,23} 
};
int n,ans;
int count(cube tmp){
    int res=0;
    if (tmp.d[0]==tmp.d[1] && tmp.d[1]==tmp.d[2] && tmp.d[2]==tmp.d[3]) res++; 
    if (tmp.d[8]==tmp.d[9] && tmp.d[14]==tmp.d[15] && tmp.d[8]==tmp.d[14]) res++;   
 if (tmp.d[6]==tmp.d[7] && tmp.d[7]==tmp.d[12] && tmp.d[12]==tmp.d[13]) res++;    
 if (tmp.d[4]==tmp.d[5] && tmp.d[5]==tmp.d[11] && tmp.d[11]==tmp.d[10]) res++;    
 if (tmp.d[16]==tmp.d[17] && tmp.d[17]==tmp.d[19] && tmp.d[19]==tmp.d[18]) res++; 
    if (tmp.d[20]==tmp.d[21] && tmp.d[21]==tmp.d[22] && tmp.d[22]==tmp.d[23]) res++; 
 return res;
}
cube trans(cube temp,int k){
    cube res;
 for(int i=0;i<24;i++)
  res.d[i]=temp.d[f[k][i]];
 return res;
}
void bfs(){
    queue<cube> q;
 while(!q.empty()) q.pop();
    q.push(c);
 ans=count(c);
    while(!q.empty()){
     cube now=q.front();
  q.pop();
  cube tmp;
  for(int i=0;i<6;i++){
      tmp=trans(now,i);
   tmp.cnt=now.cnt+1;
   ans=max(ans,count(tmp));
   if(tmp.cnt<n) q.push(tmp);
  }
 }
}
int main(){
 while(scanf("%d",&n)==1){
     for(int i=0;i<24;i++)
             scanf("%d",&c.d[i]);
  c.cnt=0;
  bfs();
  printf("%d\n",ans);
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值