The Roatin Games poj

来源:互联网 发布:网络电影播放器排行榜 编辑:程序博客网 时间:2024/06/05 07:52
The Roatin Games

*************************************************************************************************************************************************************************

预计用时:60minutes             实际用时:4200minutes

*************************************************************************************************************************************************************************

题目的知识点:

IDA*算法

形成时间差的原因:

(1)    没有从根本上分清题目的要求和主次

(2)    没有真正的理解解决该题目的思想

(3)    代码实现能力比较弱

(4)    思路混乱

分析该题目

分为3

(1)    1维数组来存储整个图,然后用二维数组来存储操作时是对一位数组的数据进行怎样操作的。

(2)    每次深度加1,暴力枚举每一层出现的问题。

(3)    利用估计函数进行剪枝操作(每次操作最多会对影响中间那8个数据中的一个种类)

 

思路变化成代码

(1)    估计函数

(2)    深度优先函数

(3)    深度的全局变量,步骤的存储方式

代码如下:

#include<iostream>

#include<cstdio>

using namespace std;

int min(int a,int b)

{

         returna<b?a:b;

}

int ac[8][7]={

         {1,3, 7, 12, 16, 21, 23}, // A 这一列的数字在输入数组中的下标下标从1开始  

   {2, 4, 9, 13, 18, 22, 24}, // B 这一列的数字在输入数组中的下标 下标从1开始  

   {11, 10, 9, 8, 7, 6, 5}, 

   {20, 19, 18, 17, 16, 15, 14}, 

   {24, 22, 18, 13, 9, 4, 2}, 

   {23, 21, 16, 12, 7, 3, 1}, 

   {14, 15, 16, 17, 18, 19, 20}, 

   {5, 6, 7, 8, 9, 10, 11} 

}; 

int A[30];

int step[100];

int st;

int maxH;

int check()

{

         inttc[4]={0,0,0,0};

         tc[A[7]]++;           tc[A[8]]++;           tc[A[9]]++;

         tc[A[12]]++;         tc[A[13]]++;         tc[A[18]]++;

         tc[A[16]]++;         tc[A[17]]++;

  return min(8-tc[1],min(8-tc[2],8-tc[3]));

}

bool dfs()

{

  int i,j;

  int n=check();

  if(!n)

            return true;

  if(maxH-st<n)//最少步骤大于最深步骤,返回

            return false;

  for(i=0;i<8;i++)

   {

            step[st++]=i;

            int temp=A[ac[i][0]];

            for(j=0;j<6;j++)

            {

                    A[ac[i][j]]=A[ac[i][j+1]];

            }

        A[ac[i][6]]=temp;

                    if(dfs()==true)

                             return true;

                    st--;

     temp=A[ac[i][6]];

            for(j=6;j>0;j--)

            {

                    A[ac[i][j]]=A[ac[i][j-1]];

            }

        A[ac[i][0]]=temp;

 

   }

  return false;//就是这个东西,坑了我一下午。。。

}

int main()

{

         //freopen("in.txt","r",stdin);

         inti;

   while(scanf("%d",&A[1]),A[1])

         {

      for(i=2;i<25;i++)

                      scanf("%d",&A[i]);

            if(check()==0)

            { printf("No moves needed\n");

                            printf("%d\n",A[7]);

                            continue;

            }

            st=0;

            maxH=1;

            while(dfs()==false)

                      maxH++;

            for(i=0;i<st;i++)

                      printf("%c",step[i]+'A');

            printf("\n%d\n",A[7]);

         }

         return0;

}

0 0
原创粉丝点击