POJ 2286 - The Rotation Game IDA(迭代加深搜索,DFSID)

    IDA是在迭代加深搜索中应用的...作用是用一个估价函数减少不必要的搜索...如本题...8-(中间最多的数的个数)+当前深度>卡的深度..就剪掉...因为一次移动最多使中间的某个数的个数增加一个...这里的"8-(中间最多的数的个数)"就相当于对当前状态的一个估价函数..实现了高效的剪枝...

    再一个..搜索的时候最好"轻装上阵"...之前我偷懒在DFSID(....)带了很多函数...还包括两个string...消耗很多不必要的时间....很多时候全局变量能解决的..还是尽量全局变量...

    最后通过这题说下对DFSID的感悟...今天是在想DFSID的优越性在哪...这题很好的体现了..

             1、不需要Hash....这道题如果要Hash...麻烦...而且非常消耗时间..我最开始就是将一串转为string..丢到set里去..秒跪...

             2、方便剪枝...IDA估价函数.只要能想到..真的很快

             3、节约空间..


Program:

#include<iostream>
#include<stack>
#include<queue>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<cmath>
#define ll long long
#define oo 1000000007
#define MAXN 100010
using namespace std; 
int a[30],ansm; 
char way[1005];
bool judge()
{
      if (a[7]==a[8]  && a[7]==a[9]  && a[7]==a[12] &&
          a[7]==a[13] && a[7]==a[16] && a[7]==a[17] && a[7]==a[18]) return true;
      return false;
}
int cnt()
{
      int S[4];
      S[1]=S[2]=S[3]=0;
      S[a[7]]++,S[a[8]]++,S[a[9]]++,S[a[12]]++;
      S[a[13]]++,S[a[16]]++,S[a[17]]++,S[a[18]]++;
      return 8-max(S[1],max(S[2],S[3]));
}
bool DFSID(int p,int t)
{ 
      if (p+cnt()>t) return false; 
      int x[30];
      if (judge())
      {
            ansm=a[7];
            return true;
      } 
      memcpy(x,a,sizeof(x));
      //-----------A------------- 
      a[1]=x[3],a[3]=x[7],a[7]=x[12],a[12]=x[16],a[16]=x[21],a[21]=x[23],a[23]=x[1];
      way[p+1]='A';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      //-----------B------------- 
      a[2]=x[4],a[4]=x[9],a[9]=x[13],a[13]=x[18],a[18]=x[22],a[22]=x[24],a[24]=x[2];
      way[p+1]='B';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      //-----------C------------- 
      a[11]=x[10],a[10]=x[9],a[9]=x[8],a[8]=x[7],a[7]=x[6],a[6]=x[5],a[5]=x[11];
      way[p+1]='C';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      //-----------D------------- 
      a[20]=x[19],a[19]=x[18],a[18]=x[17],a[17]=x[16],a[16]=x[15],a[15]=x[14],a[14]=x[20];
      way[p+1]='D';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      //-----------E------------- 
      a[24]=x[22],a[22]=x[18],a[18]=x[13],a[13]=x[9],a[9]=x[4],a[4]=x[2],a[2]=x[24];
      way[p+1]='E';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      //-----------F------------- 
      a[23]=x[21],a[21]=x[16],a[16]=x[12],a[12]=x[7],a[7]=x[3],a[3]=x[1],a[1]=x[23];
      way[p+1]='F';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      //-----------G------------- 
      a[14]=x[15],a[15]=x[16],a[16]=x[17],a[17]=x[18],a[18]=x[19],a[19]=x[20],a[20]=x[14];
      way[p+1]='G';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      //-----------H------------- 
      a[5]=x[6],a[6]=x[7],a[7]=x[8],a[8]=x[9],a[9]=x[10],a[10]=x[11],a[11]=x[5];
      way[p+1]='H';
      if (DFSID(p+1,t)) return true;
      memcpy(a,x,sizeof(x));
      return false;
}
int main()
{
      int i,x;  
      while (~scanf("%d",&a[1]) && a[1])
      {
               for (i=2;i<=24;i++) scanf("%d",&a[i]); 
               if (judge()) printf("No moves needed\n%d\n",a[7]);
                else
                {
                       for (i=1;i<=1000;i++) 
                          if (DFSID(0,i)) break; 
                       for (x=1;x<=i;x++) printf("%c",way[x]);
                       printf("\n%d\n",ansm);
                }
      }
      return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值