HDU1667 : The Rotation Game

考虑枚举最后中间的数字,然后可以用一个24位的整数来表示一个状态,一共有C(24,8)=735471种状态,然后BFS即可。

比赛的时候由于手速问题没写完TAT

写完后在坑爹评测机上还是TLE。

所以这是经过大量常数优化后的代码。

#include<cstdio>
struct P{int a,b,c,d;P(){}P(int _a,int _b,int _c,int _d){a=_a,b=_b,c=_c,d=_d;}}q[800000],x;
bool v[1<<24];
int i,j,k,S,a[24],ans,op[800000],mid,now[800000],h,t,Case;
void solve(int num){
  for(i=S=0;i<24;i++)if(a[i]==num)S|=1<<i;
  if(S==235968){
    if(ans)ans=0,mid=num;
    return;
  }
  v[S]=1,q[h=t=1]=P(S,0,0,0);
  while(h<=t){
    x=q[h],x.b++,x.d=h++;
    if(x.b>ans)break;
    int U=x.a;
    S=(U&11499450)|(U>>2&1)|((U>>6&1)<<2)|((U>>11&1)<<6)|((U>>15&1)<<11)|((U>>20&1)<<15)|((U>>22&1)<<20)|((U&1)<<22);
    if(!v[S]){
      q[++t]=P(S,x.b,0,x.d),v[S]=1;
      if(S==235968)break;
    }
    S=(U&6156021)|((U>>3&1)<<1)|((U>>8&1)<<3)|((U>>12&1)<<8)|((U>>17&1)<<12)|((U>>21&1)<<17)|((U>>23&1)<<21)|((U>>1&1)<<23);
    if(!v[S]){
      q[++t]=P(S,x.b,1,x.d),v[S]=1;
      if(S==235968)break;
    }
    S=(U&16775183)|((U&1008)<<1)|((U>>10&1)<<4);
    if(!v[S]){
      q[++t]=P(S,x.b,2,x.d),v[S]=1;
      if(S==235968)break;
    }
    S=(U&15736831)|((U&516096)<<1)|((U>>19&1)<<13);
    if(!v[S]){
      q[++t]=P(S,x.b,3,x.d),v[S]=1;
      if(S==235968)break;
    }
    S=(U&6156021)|((U>>21&1)<<23)|((U>>17&1)<<21)|((U>>12&1)<<17)|((U>>8&1)<<12)|((U>>3&1)<<8)|((U>>1&1)<<3)|((U>>23&1)<<1);
    if(!v[S]){
      q[++t]=P(S,x.b,4,x.d),v[S]=1;
      if(S==235968)break;
    }
    S=(U&11499450)|((U>>20&1)<<22)|((U>>15&1)<<20)|((U>>11&1)<<15)|((U>>6&1)<<11)|((U>>2&1)<<6)|((U&1)<<2)|(U>>22&1);
    if(!v[S]){
      q[++t]=P(S,x.b,5,x.d),v[S]=1;
      if(S==235968)break;
    }
    S=(U&15736831)|((U&1032192)>>1)|((U>>13&1)<<19);
    if(!v[S]){
      q[++t]=P(S,x.b,6,x.d),v[S]=1;
      if(S==235968)break;
    }
    S=(U&16775183)|((U&2016)>>1)|((U>>4&1)<<10);
    if(!v[S]){
      q[++t]=P(S,x.b,7,x.d),v[S]=1;
      if(S==235968)break;
    }
  }
  for(i=1;i<=t;i++){
    if(q[i].a==235968){
      if(q[i].b<ans){
        ans=q[i].b;
        for(j=ans,k=i;j;j--,k=q[k].d)op[j]=q[k].c;
        mid=num;
      }else if(q[i].b==ans){
        for(j=ans,k=i;j;j--,k=q[k].d)now[j]=q[k].c;
        int flag=0;
        for(j=1;j<=ans;j++)if(op[j]!=now[j]){
          if(op[j]>now[j])flag=1;
          else flag=0;
          break;
        }
        if(flag){
          for(j=1;j<=ans;j++)op[j]=now[j];
          mid=num;
        }
      }
    }
    v[q[i].a]=0;
  }
}
int main(){
  while(~scanf("%d",&a[0])){
    if(!a[0])return 0;
    for(i=1;i<24;i++)scanf("%d",&a[i]);
    ans=~0U>>1;
    solve(2),solve(1),solve(3);
    if(!ans)puts("No moves needed");else{
      for(i=1;i<=ans;i++)putchar(op[i]+‘A‘);
      puts("");
    }
    printf("%d\n",mid);
  }
}

  

时间: 2024-12-15 08:17:06

HDU1667 : The Rotation Game的相关文章

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

(IDA*)POJ 2286 The Rotation Game

The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind. Initially, the blocks are placed on the board randomly. Your task is to

outdated: 4.Rotation

修改部分位于双行星号内. 1 #include <windows.h> 2 #include <gl/glew.h> 3 #include <gl/glut.h> 4 #include <GL/GLAUX.H> 5 6 /* 7 * Every OpenGL program is linked to a Rendering Context. 8 * A Rendering Context is what links OpenGL calls to the D

HDU 4708 Rotation Lock Puzzle (贪心+模拟)

Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1668    Accepted Submission(s): 530 Problem Description Alice was felling into a cave. She found a strange door with a number

hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Alice was felling into a cave. She found a strange door with a number square m

【HackerRank】Game Of Rotation

题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1

Centos 7周期性任务、日志rotation、过滤和core压缩的实现

Centos 7周期性任务日志rotation.过滤和core压缩的实现 1.centos7中cron脚本及其调用脚本的实现示例 [[email protected] cron.d]# pwd /etc/cron.d [[email protected] cron.d]# cat syslog # Run mysys activity accounting tool every 10 minutes */5 * * * * root /usr/sbin/mysyslog.sh 2>&1 &g

POJ2286 The Rotation Game

The Rotation Game Time Limit: 15000MS   Memory Limit: 150000KB   64bit IO Format: %I64d & %I64u Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3,

outdated: 48.ArcBall Rotation

认认真真地把这教程过一遍还真是费事啊... 终于到最后一章了,鼠标拖动可实现物体旋转. 下面为代码, #ifndef _ArcBall_h #define _ArcBall_h // Only support assertions in debug builds #ifdef _DEBUG #include <assert.h> #else #define assert(x) { } #endif // _DEBUG #include <GL\glut.h> #include &l