zoukankan      html  css  js  c++  java
  • 【POJ2286】The Rotation Game-迭代加深DFS+可行性剪枝

    测试地址:The Rotation Game

    题目大意:一个井字形的棋盘上有8个1,8个2,8个3(图的话测试地址里面有),有8个操作(标号为A~H),表示把某一行或某一列做旋转变换,例如A方向所对的列:A <- 1 1 1 2 2 2 3 3,进行A操作后就是:A <- 1 1 2 2 2 3 3 1。要使最中心的八个方块的数字相同,求出字典序最小的操作序列(如果无需操作,输入“No moves needed”)和最后中心八个方块上的数字。

    做法:迭代加深搜索,从0开始增大搜索上限,然后就可以DFS了。

    然而,直接裸着写会TLE,那么怎么办呢?可以做可行性剪枝。我们知道每做一次操作最多只能减少1个和其他不同的数字,也就是说,如果中心八个方块出现最多的数字个数是n,那么就至少需要(8-n)次才能转变为目标状态,所以,如果当前步数加上(8-n)大于搜索上限,那么就可以剪掉。

    还有一个小小的优化,不知道有没有效果,就是记录一个操作的反操作(就是操作的是同一行(或列)而方向相反的操作),并记录上一步的操作,如果当前枚举到的是上一步操作的反操作,那么就没有意义(相当于又移回上上步的状态了),可以剪掉。

    然后,再简单处理一下旋转操作就可以过了。

    以下是本人代码:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define inf 999999999
    using namespace std;
    int a[30],ans,route[310];
    int move[8][7]=
    {
      {1,3,7,12,16,21,23},
      {2,4,9,13,18,22,24},
      {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}
    },back[9]={5,4,7,6,1,0,3,2,8},checklist[8]={7,8,9,12,13,16,17,18};
    
    int read()
    {
      int i=1;
      while(i<=24&&scanf("%d",&a[i])!=EOF) i++;
      return i-1;
    }
    
    bool check()
    {
      bool flag=1;
      for(int i=0;i<=7;i++)
        if (a[checklist[i]]!=a[7]) {flag=0;break;}
      return flag;
    }
    
    void swapper(int dir)
    {
      int temp=a[move[dir][0]];
      for(int i=1;i<=6;i++)
        a[move[dir][i-1]]=a[move[dir][i]];
      a[move[dir][6]]=temp;
    }
    
    bool f(int step)
    {
      int stat[4]={0},m=0;
      for(int i=0;i<=7;i++)
      {
        stat[a[checklist[i]]]++;
        if (stat[a[checklist[i]]]>m) m=stat[a[checklist[i]]];
      }
      return step+8-m<=ans;
    }
    
    bool dfs(int last,int step)
    {
      if (step==ans)
      {
        if (check())
    	{
    	  if (step==0) {printf("No moves needed
    ");}
    	  else
    	  {
    	    for(int i=1;i<=step;i++)
    	      printf("%c",route[i]);
    		printf("
    ");
    	  }
    	  return 1;
    	}
    	else return 0;
      }
      for(int i=0;i<8;i++)
        if (i!=back[last])
    	{
    	  swapper(i);route[step+1]='A'+i;
    	  if (f(step)&&dfs(i,step+1)) return 1;
    	  swapper(back[i]);
    	}
      return 0;
    }
    
    int main()
    {
      while(read()!=1)
      {
        for(ans=0;ans<=inf;ans++)
    	  if (dfs(8,0)) break;
    	printf("%d
    ",a[7]);
      }
      
      return 0;
    }
    


  • 相关阅读:
    为什么C/C++语言使用指针
    VS2010调试入门指南
    vs2010的11个调试技巧和方法
    排序算法一:快速排序
    Ubuntu下codeblocks汉化
    Ubuntu下Code::Blocks无法编译 /bin/sh: 1: g++ not found 解决办法
    QQ通信原理及QQ是怎么穿透内网进行通信的?
    jackson中自定义处理序列化和反序列化
    Json解析工具Jackson(使用注解)
    jackSon注解– @JsonInclude 注解不返回null值字段
  • 原文地址:https://www.cnblogs.com/Maxwei-wzj/p/9793894.html
Copyright © 2011-2022 走看看