用于三维的c++代码,在我的电脑上单线程算到11层需要3秒。
要算更多层,只需要更改const int maxl=层数;
- #include<stdint.h>
- #include<stdio.h>
- const int maxl=11;
- int64_t in[64]={0},on[64]={0},out[64]={0};
- int64_t g;
- #define xm(proc,x,y,z) \
- proc(x-2,y-2,z-2,2*x+2*y+2*z+6,0); \
- proc(x-2,y-2,z,2*x+2*y+z+6,z-2); \
- proc(x-2,y-2,z+2,2*x+2*y+4,2*z-2); \
- proc(x-2,y,z-2,2*x+y+2*z+6,y-2); \
- proc(x-2,y,z+2,2*x+y+4,y+2*z-4); \
- proc(x-2,y+2,z-2,2*x+2*z+4,2*y-2); \
- proc(x-2,y+2,z,2*x+z+4,2*y+z-4); \
- proc(x-2,y+2,z+2,2*x+2,2*y+2*z-4);
- #define x0(proc,x,y,z) \
- proc(x,y-2,z-2,x+2*y+2*z+6,x-2); \
- proc(x,y-2,z+2,x+2*y+4,x+2*z-4); \
- proc(x,y+2,z-2,x+2*z+4,x+2*y-4); \
- proc(x,y+2,z+2,x+2,x+2*y+2*z-6);
- #define xp(proc,x,y,z) \
- proc(x+2,y-2,z-2,2*y+2*z+4,2*x-2); \
- proc(x+2,y-2,z,2*y+z+4,2*x+z-4); \
- proc(x+2,y-2,z+2,2*y+2,2*x+2*z-4); \
- proc(x+2,y,z-2,y+2*z+4,2*x+y-4); \
- proc(x+2,y,z+2,y+2,2*x+y+2*z-6); \
- proc(x+2,y+2,z-2,2*z+2,2*x+2*y-4); \
- proc(x+2,y+2,z,z+2,2*x+2*y+z-6); \
- proc(x+2,y+2,z+2,0,2*x+2*y+2*z-6);
- void ffull(int l,int64_t x,int64_t y,int64_t z,int64_t fh,int64_t fl){
- ++l;
- x*=3;
- y*=3;
- z*=3;
- fh*=9;
- fl*=9;
- int64_t nfh,nfl;
- #define proc(nx,ny,nz,dfh,dfl) \
- do{ \
- nfh=fh-4*(dfh),nfl=fl+4*(dfl); \
- if(nfl>=0)out[l]+=g; \
- else if(nfh<=0)in[l]+=g; \
- else { \
- on[l]+=g; \
- if(l<maxl) \
- ffull(l,nx,ny,nz,nfh,nfl); \
- } \
- }while(0)
- xm(proc,x,y,z);
- x0(proc,x,y,z);
- xp(proc,x,y,z);
- }
- void fhalf(int l,int64_t y,int64_t z,int64_t fh,int64_t fl){
- const int64_t x=0;
- ++l;
- y*=3;
- z*=3;
- fh*=9;
- fl*=9;
- int64_t nfh,nfl;
- #define proch(nx,ny,nz,dfh,dfl) \
- do{ \
- nfh=fh-4*(dfh),nfl=fl+4*(dfl); \
- if(nfl>=0)out[l]+=g/2; \
- else if(nfh<=0)in[l]+=g/2; \
- else { \
- on[l]+=g/2; \
- if(l<maxl) \
- fhalf(l,ny,nz,nfh,nfl); \
- } \
- }while(0)
- x0(proch,x,y,z);
- xp(proc,x,y,z);
- }
- int main(){
- on[0]=1;
- on[1]=20;
- g=8;
- ffull(1,2,2,2,18,-6);
- g=24;
- fhalf(1, 2,2,10,-6);
- for(int i=1;i<=maxl;++i){
- /*
- Note: To avoid overflow,
- in[i] & out[i] are not real counts of level i cubes
- unless after executing following lines :
- in[i]+=20*in[i-1];
- out[i]+=20*out[i-1];
- This can be done in Mathematica
- */
- printf("%llu %llu %llu\n",in[i],on[i],out[i]);
- }
- }
复制代码
|